From d909e728c6778723f6dc04d49d30e0f6e38f7e74 Mon Sep 17 00:00:00 2001 From: Brandon Maxwell Date: Tue, 24 Nov 2015 17:56:54 -0800 Subject: Compatibility method to check system settings write permission SettingsCompat.System.canWrite returns true for versions older than M because prior to M, permissions had to be granted prior to installing the app. It's only in M and above that we check for permissions at runtime. Bug:25776171 Change-Id: Iee2250248223e7237f8eaa506b277e301e289cc0 --- src/com/android/dialer/compat/SettingsCompat.java | 47 ++++++++++++++++++++++ .../dialer/settings/DefaultRingtonePreference.java | 5 +-- .../dialer/settings/DialerSettingsActivity.java | 3 +- .../dialer/settings/SoundSettingsFragment.java | 19 ++------- 4 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 src/com/android/dialer/compat/SettingsCompat.java (limited to 'src') diff --git a/src/com/android/dialer/compat/SettingsCompat.java b/src/com/android/dialer/compat/SettingsCompat.java new file mode 100644 index 000000000..474a600a4 --- /dev/null +++ b/src/com/android/dialer/compat/SettingsCompat.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2015 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.dialer.compat; + +import android.content.Context; +import android.os.Build; +import android.os.Build.VERSION_CODES; +import android.provider.Settings; + +import com.android.contacts.common.compat.SdkVersionOverride; + +/** + * Compatibility class for {@link android.provider.Settings} + */ +public class SettingsCompat { + + public static class System { + + /** + * Compatibility version of {@link android.provider.Settings.System#canWrite(Context)} + * + * Note: Since checking preferences at runtime started in M, this method always returns + * {@code true} for SDK versions prior to 23. In those versions, the app wouldn't be + * installed if it didn't have the proper permission + */ + public static boolean canWrite(Context context) { + if (SdkVersionOverride.getSdkVersion(VERSION_CODES.LOLLIPOP) >= Build.VERSION_CODES.M) { + return Settings.System.canWrite(context); + } + return true; + } + } + +} diff --git a/src/com/android/dialer/settings/DefaultRingtonePreference.java b/src/com/android/dialer/settings/DefaultRingtonePreference.java index a1743814a..a8a23fddf 100644 --- a/src/com/android/dialer/settings/DefaultRingtonePreference.java +++ b/src/com/android/dialer/settings/DefaultRingtonePreference.java @@ -16,17 +16,16 @@ package com.android.dialer.settings; -import android.app.AppOpsManager; import android.content.Context; import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; import android.preference.RingtonePreference; -import android.provider.Settings; import android.util.AttributeSet; import android.widget.Toast; import com.android.dialer.R; +import com.android.dialer.compat.SettingsCompat; /** * RingtonePreference which doesn't show default ringtone setting. @@ -49,7 +48,7 @@ public class DefaultRingtonePreference extends RingtonePreference { @Override protected void onSaveRingtone(Uri ringtoneUri) { - if (!Settings.System.canWrite(getContext())) { + if (!SettingsCompat.System.canWrite(getContext())) { Toast.makeText( getContext(), getContext().getResources().getString(R.string.toast_cannot_write_system_settings), diff --git a/src/com/android/dialer/settings/DialerSettingsActivity.java b/src/com/android/dialer/settings/DialerSettingsActivity.java index d0bfbc211..a6e8944d4 100644 --- a/src/com/android/dialer/settings/DialerSettingsActivity.java +++ b/src/com/android/dialer/settings/DialerSettingsActivity.java @@ -28,6 +28,7 @@ import android.view.MenuItem; import android.widget.Toast; import com.android.dialer.R; +import com.android.dialer.compat.SettingsCompat; import com.android.dialer.filterednumber.BlockedNumbersSettingsActivity; import java.util.List; @@ -109,7 +110,7 @@ public class DialerSettingsActivity extends AppCompatPreferenceActivity { // If we don't have the permission to write to system settings, go to system sound // settings instead. Otherwise, perform the super implementation (which launches our // own preference fragment. - if (!Settings.System.canWrite(this)) { + if (!SettingsCompat.System.canWrite(this)) { Toast.makeText( this, getResources().getString(R.string.toast_cannot_write_system_settings), diff --git a/src/com/android/dialer/settings/SoundSettingsFragment.java b/src/com/android/dialer/settings/SoundSettingsFragment.java index 35e1dae2d..30a25a384 100644 --- a/src/com/android/dialer/settings/SoundSettingsFragment.java +++ b/src/com/android/dialer/settings/SoundSettingsFragment.java @@ -16,9 +16,7 @@ package com.android.dialer.settings; -import android.app.AppOpsManager; import android.content.Context; -import android.content.Intent; import android.media.RingtoneManager; import android.os.Bundle; import android.os.Handler; @@ -32,21 +30,12 @@ import android.preference.PreferenceScreen; import android.provider.Settings; import android.telephony.CarrierConfigManager; import android.telephony.TelephonyManager; -import android.view.MenuItem; import android.widget.Toast; -import com.android.contacts.common.util.PermissionsUtil; import com.android.dialer.R; +import com.android.dialer.compat.SettingsCompat; import com.android.phone.common.util.SettingsUtil; -import java.lang.Boolean; -import java.lang.CharSequence; -import java.lang.Object; -import java.lang.Override; -import java.lang.Runnable; -import java.lang.String; -import java.lang.Thread; - public class SoundSettingsFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener { @@ -137,7 +126,7 @@ public class SoundSettingsFragment extends PreferenceFragment public void onResume() { super.onResume(); - if (!Settings.System.canWrite(getContext())) { + if (!SettingsCompat.System.canWrite(getContext())) { // If the user launches this setting fragment, then toggles the WRITE_SYSTEM_SETTINGS // AppOp, then close the fragment since there is nothing useful to do. getActivity().onBackPressed(); @@ -160,7 +149,7 @@ public class SoundSettingsFragment extends PreferenceFragment */ @Override public boolean onPreferenceChange(Preference preference, Object objValue) { - if (!Settings.System.canWrite(getContext())) { + if (!SettingsCompat.System.canWrite(getContext())) { // A user shouldn't be able to get here, but this protects against monkey crashes. Toast.makeText( getContext(), @@ -186,7 +175,7 @@ public class SoundSettingsFragment extends PreferenceFragment */ @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { - if (!Settings.System.canWrite(getContext())) { + if (!SettingsCompat.System.canWrite(getContext())) { Toast.makeText( getContext(), getResources().getString(R.string.toast_cannot_write_system_settings), -- cgit v1.2.3