Skip to content

Commit 38d5953

Browse files
Merge pull request #688 from TakayukiHoshi1984/bugfix_system_setting_window
不具合修正: Android 10 以降、画面の明るさ設定時、システム設定の画面が表示されないことがある
2 parents fc9190c + 246b8c9 commit 38d5953

4 files changed

Lines changed: 70 additions & 31 deletions

File tree

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/profile/HostPhoneProfile.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import android.os.Looper;
2020
import android.os.ResultReceiver;
2121
import androidx.annotation.NonNull;
22+
23+
import android.provider.Settings;
2224
import android.telecom.TelecomManager;
2325
import android.telephony.TelephonyManager;
2426

@@ -148,23 +150,18 @@ public boolean onRequest(final Intent request, final Intent response) {
148150
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
149151
&& !notificationManager.isNotificationPolicyAccessGranted()) {
150152

151-
Intent intent = new Intent(
152-
android.provider.Settings
153-
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
154-
155-
IntentHandlerActivity.startActivityForResult(getContext(), intent,
156-
new ResultReceiver(new Handler(Looper.getMainLooper())) {
157-
@Override
158-
protected void onReceiveResult(final int resultCode, final Bundle resultData) {
159-
if (notificationManager.isNotificationPolicyAccessGranted()) {
160-
setPhoneMode(response, mode);
161-
} else {
162-
MessageUtils.setIllegalServerStateError(response,
163-
"PHOME_MODE setting permisson not granted");
164-
}
165-
sendResponse(response);
166-
}
167-
});
153+
requestNotificationPolicyPermission(new ResultReceiver(new Handler(Looper.getMainLooper())) {
154+
@Override
155+
protected void onReceiveResult(final int resultCode, final Bundle resultData) {
156+
if (notificationManager.isNotificationPolicyAccessGranted()) {
157+
setPhoneMode(response, mode);
158+
} else {
159+
MessageUtils.setIllegalServerStateError(response,
160+
"PHOME_MODE setting permisson not granted");
161+
}
162+
sendResponse(response);
163+
}
164+
});
168165
return false;
169166
}
170167
setPhoneMode(response, mode);
@@ -173,6 +170,27 @@ protected void onReceiveResult(final int resultCode, final Bundle resultData) {
173170
}
174171
};
175172

173+
private void requestNotificationPolicyPermission(final ResultReceiver resultReceiver) {
174+
Intent intent = new Intent(
175+
android.provider.Settings
176+
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
177+
178+
Intent callIntent = new Intent(getContext(), IntentHandlerActivity.class);
179+
callIntent.putExtra("EXTRA_INTENT", intent);
180+
callIntent.putExtra("EXTRA_CALLBACK", resultReceiver);
181+
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP
182+
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
183+
184+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
185+
NotificationUtils.createNotificationChannel(getContext());
186+
NotificationUtils.notify(getContext(), NOTIFICATION_ID, 0, callIntent,
187+
getContext().getString(R.string.host_notification_setting_warnning));
188+
} else {
189+
getContext().startActivity(callIntent);
190+
}
191+
}
192+
193+
176194
private void setPhoneMode(Intent response, PhoneMode mode) {
177195
// AudioManager
178196
AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/profile/HostSettingProfile.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99

1010
import org.deviceconnect.android.activity.IntentHandlerActivity;
11+
import org.deviceconnect.android.deviceplugin.host.R;
1112
import org.deviceconnect.android.message.MessageUtils;
1213
import org.deviceconnect.android.profile.SettingProfile;
1314
import org.deviceconnect.android.profile.api.DConnectApi;
1415
import org.deviceconnect.android.profile.api.GetApi;
1516
import org.deviceconnect.android.profile.api.PutApi;
17+
import org.deviceconnect.android.util.NotificationUtils;
1618
import org.deviceconnect.message.DConnectMessage;
1719
import 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) {

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/res/values-ja/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,6 @@
117117
<string name="host_notification_geolocation_warnning">Host Geolocation Profileからの起動要求</string>
118118
<string name="host_notification_connection_warnning">Host Connection Profileからの起動要求</string>
119119
<string name="host_notification_projection_warnning">Host Media Streaming Recording Profileからの起動要求</string>
120-
<string name="host_notification_phone_warnning">Host Connection Profileからの起動要求</string>
120+
<string name="host_notification_phone_warnning">Host Phone Profileからの起動要求</string>
121+
<string name="host_notification_setting_warnning">Host Setting Profileからの起動要求</string>
121122
</resources>

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,6 @@
131131
<string name="host_notification_connection_warnning">Start request from Host Connection Profile</string>
132132
<string name="host_notification_projection_warnning">Start request from Host Media Streaming Recording Profile</string>
133133
<string name="host_notification_phone_warnning">Start request from Host Phone Profile</string>
134+
<string name="host_notification_setting_warnning">Start request from Host Setting Profile</string>
134135
</resources>
135136

0 commit comments

Comments
 (0)