88
99
1010import org .deviceconnect .android .activity .IntentHandlerActivity ;
11+ import org .deviceconnect .android .deviceplugin .host .R ;
1112import org .deviceconnect .android .message .MessageUtils ;
1213import org .deviceconnect .android .profile .SettingProfile ;
1314import org .deviceconnect .android .profile .api .DConnectApi ;
1415import org .deviceconnect .android .profile .api .GetApi ;
1516import org .deviceconnect .android .profile .api .PutApi ;
17+ import org .deviceconnect .android .util .NotificationUtils ;
1618import org .deviceconnect .message .DConnectMessage ;
1719import org .deviceconnect .utils .RFC3339DateUtils ;
1820
@@ -37,6 +39,7 @@ public class HostSettingProfile extends SettingProfile {
3739 /** Light Levelの最大値. */
3840 private static final int MAX_LIGHT_LEVEL = 255 ;
3941
42+ private final int NOTIFICATION_ID = 4000 ;
4043
4144 private final DConnectApi mGetSoundVolumeApi = new GetApi () {
4245
@@ -223,21 +226,18 @@ public boolean onRequest(final Intent request, final Intent response) {
223226 if (Settings .System .canWrite (getContext ())) {
224227 onPutDisplayLightInternal (request , response , serviceId , level );
225228 } else {
226- Intent intent = new Intent (Settings .ACTION_MANAGE_WRITE_SETTINGS ,
227- Uri .parse ("package:" + getContext ().getPackageName ()));
228- IntentHandlerActivity .startActivityForResult (getContext (), intent ,
229- new ResultReceiver (new Handler (Looper .getMainLooper ())) {
230- @ Override
231- protected void onReceiveResult (final int resultCode , final Bundle resultData ) {
232- if (Settings .System .canWrite (getContext ())) {
233- onPutDisplayLightInternal (request , response , serviceId , level );
234- } else {
235- MessageUtils .setIllegalServerStateError (response ,
229+ requestSystemSettingPermission (new ResultReceiver (new Handler (Looper .getMainLooper ())) {
230+ @ Override
231+ protected void onReceiveResult (final int resultCode , final Bundle resultData ) {
232+ if (Settings .System .canWrite (getContext ())) {
233+ onPutDisplayLightInternal (request , response , serviceId , level );
234+ } else {
235+ MessageUtils .setIllegalServerStateError (response ,
236236 "WRITE_SETTINGS permisson not granted" );
237- }
238- sendResponse (response );
239237 }
240- });
238+ sendResponse (response );
239+ }
240+ });
241241 return false ;
242242 }
243243 } else {
@@ -247,6 +247,25 @@ protected void onReceiveResult(final int resultCode, final Bundle resultData) {
247247 }
248248 };
249249
250+ private void requestSystemSettingPermission (final ResultReceiver resultReceiver ) {
251+ Intent intent = new Intent (Settings .ACTION_MANAGE_WRITE_SETTINGS ,
252+ Uri .parse ("package:" + getContext ().getPackageName ()));
253+
254+ Intent callIntent = new Intent (getContext (), IntentHandlerActivity .class );
255+ callIntent .putExtra ("EXTRA_INTENT" , intent );
256+ callIntent .putExtra ("EXTRA_CALLBACK" , resultReceiver );
257+ callIntent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_SINGLE_TOP
258+ | Intent .FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS );
259+
260+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
261+ NotificationUtils .createNotificationChannel (getContext ());
262+ NotificationUtils .notify (getContext (), NOTIFICATION_ID , 0 , callIntent ,
263+ getContext ().getString (R .string .host_notification_setting_warnning ));
264+ } else {
265+ getContext ().startActivity (callIntent );
266+ }
267+ }
268+
250269 private final DConnectApi mPutDisplaySleepApi = new PutApi () {
251270 @ Override
252271 public boolean onRequest (final Intent request , final Intent response ) {
0 commit comments