Move the permission warning message to corresponding layout file.
Bug: 129284701 Test: Manually verified Test: atest AdaptiveSleepPermissionPreferenceController Change-Id: Ic31bdb37f751288ae6dcd1253d56806c767e2e47
This commit is contained in:
@@ -21,11 +21,9 @@ import android.content.Context;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
public class AdaptiveSleepDetailPreferenceController extends AdaptiveSleepPreferenceController {
|
||||
private final Context mContext;
|
||||
|
||||
public AdaptiveSleepDetailPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -45,7 +43,6 @@ public class AdaptiveSleepDetailPreferenceController extends AdaptiveSleepPrefer
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
preference.setEnabled(AdaptiveSleepPreferenceController.hasSufficientPermission(
|
||||
mContext.getPackageManager()));
|
||||
preference.setEnabled(hasSufficientPermission(mContext.getPackageManager()));
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License
|
||||
*/
|
||||
|
||||
package com.android.settings.display;
|
||||
|
||||
import static com.android.settings.display.AdaptiveSleepPreferenceController.hasSufficientPermission;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
|
||||
public class AdaptiveSleepPermissionPreferenceController extends BasePreferenceController {
|
||||
final static String PREF_NAME = "adaptive_sleep_permission";
|
||||
private final Intent mIntent;
|
||||
|
||||
public AdaptiveSleepPermissionPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
final String packageName = context.getPackageManager().getAttentionServicePackageName();
|
||||
mIntent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||
mIntent.setData(Uri.parse("package:" + packageName));
|
||||
}
|
||||
|
||||
@Override
|
||||
@AvailabilityStatus
|
||||
public int getAvailabilityStatus() {
|
||||
return AVAILABLE_UNSEARCHABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
if (TextUtils.equals(getPreferenceKey(), preference.getKey())) {
|
||||
mContext.startActivity(mIntent);
|
||||
return true;
|
||||
}
|
||||
return super.handlePreferenceTreeClick(preference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
if (TextUtils.equals(getPreferenceKey(), preference.getKey())) {
|
||||
preference.setVisible(!hasSufficientPermission(mContext.getPackageManager()));
|
||||
}
|
||||
}
|
||||
}
|
@@ -16,21 +16,16 @@
|
||||
|
||||
package com.android.settings.display;
|
||||
|
||||
import static com.android.settings.display.AdaptiveSleepPreferenceController.hasSufficientPermission;
|
||||
import static com.android.settings.homepage.contextualcards.slices.ContextualAdaptiveSleepSlice.PREF;
|
||||
import static com.android.settings.homepage.contextualcards.slices.ContextualAdaptiveSleepSlice.PREF_KEY_INTERACTED;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.SearchIndexableResource;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
@@ -45,11 +40,6 @@ public class AdaptiveSleepSettings extends DashboardFragment {
|
||||
|
||||
private static final String TAG = "AdaptiveSleepSettings";
|
||||
private Context mContext;
|
||||
private String mPackageName;
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
@VisibleForTesting
|
||||
Preference mPermissionRequiredPreference;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
@@ -57,32 +47,22 @@ public class AdaptiveSleepSettings extends DashboardFragment {
|
||||
final FooterPreference footerPreference =
|
||||
mFooterPreferenceMixin.createFooterPreference();
|
||||
mContext = getContext();
|
||||
mPermissionRequiredPreference = createPermissionMissionPreference();
|
||||
|
||||
footerPreference.setIcon(R.drawable.ic_privacy_shield_24dp);
|
||||
footerPreference.setTitle(R.string.adaptive_sleep_privacy);
|
||||
|
||||
getPreferenceScreen().addPreference(mPermissionRequiredPreference);
|
||||
mPermissionRequiredPreference.setVisible(false);
|
||||
mPackageManager = mContext.getPackageManager();
|
||||
mPackageName = mPackageManager.getAttentionServicePackageName();
|
||||
Preference permissionPreference = findPreference(
|
||||
AdaptiveSleepPermissionPreferenceController.PREF_NAME);
|
||||
if (permissionPreference != null) {
|
||||
permissionPreference.setVisible(false);
|
||||
}
|
||||
|
||||
mContext.getSharedPreferences(PREF, Context.MODE_PRIVATE)
|
||||
.edit()
|
||||
.putBoolean(PREF_KEY_INTERACTED, true)
|
||||
.apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (!hasSufficientPermission(mPackageManager)) {
|
||||
mPermissionRequiredPreference.setVisible(true);
|
||||
}
|
||||
else {
|
||||
mPermissionRequiredPreference.setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.adaptive_sleep_detail;
|
||||
@@ -113,29 +93,4 @@ public class AdaptiveSleepSettings extends DashboardFragment {
|
||||
return Arrays.asList(sir);
|
||||
}
|
||||
};
|
||||
|
||||
private Preference createPermissionMissionPreference() {
|
||||
Preference preference = new Preference(mContext, null);
|
||||
preference.setIcon(R.drawable.ic_info_outline_24);
|
||||
// Makes sure it's above the toggle.
|
||||
preference.setOrder(1);
|
||||
preference.setPersistent(true);
|
||||
preference.setTitle(R.string.adaptive_sleep_title_no_permission);
|
||||
preference.setSummary(R.string.adaptive_sleep_summary_no_permission);
|
||||
preference.setOnPreferenceClickListener(p -> {
|
||||
final Intent intent = new Intent(
|
||||
android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||
intent.setData(Uri.parse("package:" + mPackageName));
|
||||
mContext.startActivity(intent);
|
||||
return true;
|
||||
});
|
||||
return preference;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setupForTesting(PackageManager packageManager, Context context) {
|
||||
mContext = context;
|
||||
mPackageManager = packageManager;
|
||||
mPermissionRequiredPreference = createPermissionMissionPreference();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user