Skip to content

Commit

Permalink
Update Android 6.0 sample
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshihideSogawa committed Oct 26, 2015
1 parent 3a3d329 commit 3b9a247
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ allprojects {
}

ext {
compileSdkVersion = 22
buildToolsVersion = '22.0.1'
targetSdkVersion = 22
compileSdkVersion = 23
buildToolsVersion = '23.0.1'
targetSdkVersion = 23
minSdkVersion = 14
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package jp.co.recruit_lifestyle.sample.fragment;

import android.app.Activity;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -25,6 +30,16 @@ public class FloatingViewControlFragment extends Fragment {
*/
private CountDownTimer mTimer;

/**
* シンプルなFloatingViewを表示するフローのパーミッション許可コード
*/
private static final int CHATHEAD_OVERLAY_PERMISSION_REQUEST_CODE = 100;

/**
* 広告連携のFloatingViewを表示するフローのパーミッション許可コード
*/
private static final int FLOATINGAD_OVERLAY_PERMISSION_REQUEST_CODE = 101;

/**
* FloatingViewControlFragmentを生成します。
*/
Expand Down Expand Up @@ -56,8 +71,14 @@ public void onTick(long millisUntilFinished) {

@Override
public void onFinish() {
final Activity activity = getActivity();
activity.startService(new Intent(activity, FloatingAdService.class));
final Context context = getActivity();
final boolean canShow = showFloatingAd(context);
if (!canShow) {
// 広告トリガーのFloatingViewの表示許可設定
@SuppressLint("InlinedApi")
final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.getPackageName()));
startActivityForResult(intent, FLOATINGAD_OVERLAY_PERMISSION_REQUEST_CODE);
}
}
};

Expand All @@ -73,8 +94,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
rootView.findViewById(R.id.showDemo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Activity activity = getActivity();
activity.startService(new Intent(activity, ChatHeadService.class));
final Context context = getActivity();
final boolean canShow = showChatHead(context);
if (!canShow) {
// シンプルなFloatingViewの表示許可設定
@SuppressLint("InlinedApi")
final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.getPackageName()));
startActivityForResult(intent, CHATHEAD_OVERLAY_PERMISSION_REQUEST_CODE);
}
}
});
// 広告の表示
Expand All @@ -88,6 +115,27 @@ public void onClick(View v) {
return rootView;
}

/**
* オーバレイ表示の許可を処理します。
*/
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CHATHEAD_OVERLAY_PERMISSION_REQUEST_CODE) {
final Context context = getActivity();
final boolean canShow = showChatHead(context);
if (!canShow) {
Toast.makeText(context, R.string.permission_denied, Toast.LENGTH_LONG).show();
}
} else if (requestCode == FLOATINGAD_OVERLAY_PERMISSION_REQUEST_CODE) {
final Context context = getActivity();
final boolean canShow = showFloatingAd(context);
if (!canShow) {
Toast.makeText(context, R.string.permission_denied, Toast.LENGTH_LONG).show();
}
}
}

/**
* {@inheritDoc}
*/
Expand All @@ -98,4 +146,50 @@ public void onDestroy() {
}
super.onDestroy();
}

/**
* シンプルなFloatingViewの表示
*
* @param context Context
* @return 表示できる場合はtrue, 表示できない場合はfalse
*/
@SuppressLint("NewApi")
private boolean showChatHead(Context context) {
// API23未満かチェック
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
context.startService(new Intent(context, ChatHeadService.class));
return true;
}

// 他のアプリの上に表示できるかチェック
if (Settings.canDrawOverlays(context)) {
context.startService(new Intent(context, ChatHeadService.class));
return true;
}

return false;
}

/**
* 広告表示のFloatingViewの表示
*
* @param context Context
* @return 表示できる場合はtrue, 表示できない場合はfalse
*/
@SuppressLint("NewApi")
private boolean showFloatingAd(Context context) {
// API23未満かチェック
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
context.startService(new Intent(context, FloatingAdService.class));
return true;
}

// 他のアプリの上に表示できるかチェック
if (Settings.canDrawOverlays(context)) {
context.startService(new Intent(context, FloatingAdService.class));
return true;
}

return false;
}
}
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
<string name="show_demo">デモ表示</string>
<string name="show_ad">広告表示</string>
<string name="ad_unit_id">ADD_YOUR_UNIT_ID</string>
<string name="permission_denied">オーバレイの許可が出ませんでした</string>
</resources>

0 comments on commit 3b9a247

Please sign in to comment.