diff options
author | Kedi Xu <kedix@codeaurora.org> | 2016-10-12 15:52:54 +0800 |
---|---|---|
committer | Kedi Xu <kedix@codeaurora.org> | 2016-10-12 15:59:20 +0800 |
commit | 3d616518707620f13f992033f97b845e414996c4 (patch) | |
tree | 2fb05f5d66340bc36cc9adde2c655b1271ffe107 /src/org | |
parent | 6948304f6919e81b75b5a1fa9f86d783e9fb87dd (diff) | |
download | android_packages_apps_Gallery2-3d616518707620f13f992033f97b845e414996c4.tar.gz android_packages_apps_Gallery2-3d616518707620f13f992033f97b845e414996c4.tar.bz2 android_packages_apps_Gallery2-3d616518707620f13f992033f97b845e414996c4.zip |
SnapdragonGallery: disable set APN when no SIM card
When no SIM card inserted, remove apn related preference
Change-Id: Ifd79b1018f4658acf1a9ff87c4762a84c131de0e
CRs-Fixed: 1066720
Diffstat (limited to 'src/org')
-rw-r--r-- | src/org/codeaurora/gallery3d/video/AbstractPermissionPreferenceActivity.java | 116 | ||||
-rw-r--r-- | src/org/codeaurora/gallery3d/video/SettingsActivity.java | 93 |
2 files changed, 208 insertions, 1 deletions
diff --git a/src/org/codeaurora/gallery3d/video/AbstractPermissionPreferenceActivity.java b/src/org/codeaurora/gallery3d/video/AbstractPermissionPreferenceActivity.java new file mode 100644 index 000000000..a6be5afeb --- /dev/null +++ b/src/org/codeaurora/gallery3d/video/AbstractPermissionPreferenceActivity.java @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2016, The Linux Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.codeaurora.gallery3d.video; + +import android.Manifest; +import android.content.pm.PackageManager; +import android.os.Build; +import android.os.Bundle; +import android.preference.PreferenceActivity; + +import java.util.ArrayList; + +public abstract class AbstractPermissionPreferenceActivity extends PreferenceActivity { + + public static final int PERMISSION_REQUEST_PHONE = 1; + private boolean permissionGranted = false; + + protected abstract void onGetPermissionsSuccess(); + protected abstract void onGetPermissionsFailure(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + requestPhonePermission(); + } + + private void requestPhonePermission() { + String[] permissions = { + Manifest.permission.READ_PHONE_STATE + }; + requestPermission(permissions, PERMISSION_REQUEST_PHONE); + } + + protected void requestPermission(String[] permissions, int requestCode) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + permissionGranted = true; + return; + } + + boolean needRequest = false; + ArrayList<String> permissionList = new ArrayList<String>(); + for (String permission : permissions) { + if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { + permissionList.add(permission); + needRequest = true; + } + } + + if (needRequest) { + int count = permissionList.size(); + if (count > 0) { + String[] permissionArray = new String[count]; + for (int i = 0; i < count; i++) { + permissionArray[i] = permissionList.get(i); + } + + requestPermissions(permissionArray, requestCode); + } + } + permissionGranted = !needRequest; + } + + private boolean checkPermissionGrantResults(int[] grantResults) { + for (int result : grantResults) { + if (result != PackageManager.PERMISSION_GRANTED) { + return false; + } + } + return true; + } + + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, + int[] grantResults) { + permissionGranted = checkPermissionGrantResults(grantResults); + switch (requestCode) { + case PERMISSION_REQUEST_PHONE: { + if (permissionGranted) { + onGetPermissionsSuccess(); + } else { + onGetPermissionsFailure(); + } + } + } + } + + protected boolean isPermissionGranted() { + return permissionGranted; + } +} diff --git a/src/org/codeaurora/gallery3d/video/SettingsActivity.java b/src/org/codeaurora/gallery3d/video/SettingsActivity.java index b0bd65c32..f2b1bebac 100644 --- a/src/org/codeaurora/gallery3d/video/SettingsActivity.java +++ b/src/org/codeaurora/gallery3d/video/SettingsActivity.java @@ -9,9 +9,13 @@ import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.PreferenceActivity; +import android.preference.PreferenceCategory; +import android.preference.PreferenceGroup; import android.preference.PreferenceScreen; import android.provider.Settings; import android.provider.Settings.System; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; import android.text.method.DigitsKeyListener; import android.util.Log; @@ -22,8 +26,9 @@ import com.android.gallery3d.common.ApiHelper; import com.android.gallery3d.common.ApiHelper.SystemProperties; import java.util.ArrayList; +import java.util.List; -public class SettingsActivity extends PreferenceActivity { +public class SettingsActivity extends AbstractPermissionPreferenceActivity { private static final String LOG_TAG = "SettingsActivity"; @@ -33,6 +38,7 @@ public class SettingsActivity extends PreferenceActivity { private static final String PREFERENCE_CACHE_MIN_SIZE = "cache_min_size"; private static final String PREFERENCE_CACHE_MAX_SIZE = "cache_max_size"; public static final String PREFERENCE_BUFFER_SIZE = "buffer_size"; + public static final String PREFERENCE_APN_CATEGORY = "apn_category"; public static final String PREFERENCE_APN = "apn"; private static final String PACKAGE_NAME = "com.android.settings"; @@ -45,11 +51,14 @@ public class SettingsActivity extends PreferenceActivity { private static final int RTP_MIN_PORT = 1; private static final int RTP_MAX_PORT = 2; private static final int BUFFER_SIZE = 3; + private static final boolean DBG = true; + private static final String TAG = SettingsActivity.class.getSimpleName(); private SharedPreferences mPref; private EditTextPreference mRtpMinPort; private EditTextPreference mRtpMaxPort; private EditTextPreference mBufferSize; + private PreferenceCategory mApnCategory; private PreferenceScreen mApn; private static final int SELECT_APN = 1; @@ -61,6 +70,19 @@ public class SettingsActivity extends PreferenceActivity { private boolean mUseNvOperatorForEhrpd = SystemProperties.getBoolean( "persist.radio.use_nv_for_ehrpd", false); + private SubscriptionManager mSubscriptionManager; + private List<SubscriptionInfo> mActiveSubInfos; + + @Override + protected void onGetPermissionsSuccess() { + init(); + } + + @Override + protected void onGetPermissionsFailure() { + finish(); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -70,6 +92,7 @@ public class SettingsActivity extends PreferenceActivity { mRtpMinPort = (EditTextPreference) findPreference(PREFERENCE_RTP_MINPORT); mRtpMaxPort = (EditTextPreference) findPreference(PREFERENCE_RTP_MAXPORT); mBufferSize = (EditTextPreference) findPreference(PREFERENCE_BUFFER_SIZE); + mApnCategory = (PreferenceCategory) findPreference(PREFERENCE_APN_CATEGORY); mApn = (PreferenceScreen) findPreference(PREFERENCE_APN); setPreferenceListener(RTP_MIN_PORT, mRtpMinPort); @@ -81,6 +104,74 @@ public class SettingsActivity extends PreferenceActivity { ab.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); ab.setDisplayHomeAsUpEnabled(true); ab.setTitle(R.string.setting); + + if (isPermissionGranted()) { + init(); + } + } + + private void init() { + mSubscriptionManager = SubscriptionManager.from(this); + mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener); + // Initialize mActiveSubInfo + int max = mSubscriptionManager.getActiveSubscriptionInfoCountMax(); + mActiveSubInfos = new ArrayList<SubscriptionInfo>(max); + + initializeSubscriptions(); + + if (!hasActiveSubscriptions()) { + ((PreferenceGroup)mApnCategory).removePreference(mApn); + getPreferenceScreen().removePreference(mApnCategory); + + } + } + + @Override + public void onPause() { + super.onPause(); + if (null != mSubscriptionManager) { + mSubscriptionManager + .removeOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener); + } + } + + private final SubscriptionManager.OnSubscriptionsChangedListener mOnSubscriptionsChangeListener + = new SubscriptionManager.OnSubscriptionsChangedListener() { + @Override + public void onSubscriptionsChanged() { + if (DBG) Log.i(TAG, "onSubscriptionsChanged:"); + initializeSubscriptions(); + if (hasActiveSubscriptions()) { + getPreferenceScreen().addPreference(mApnCategory); + mApnCategory.addPreference(mApn); + } else{ + ((PreferenceGroup)mApnCategory).removePreference(mApn); + getPreferenceScreen().removePreference(mApnCategory); + } + } + }; + + private void initializeSubscriptions() { + if (isDestroyed()) { // Process preferences in activity only if its not destroyed + return; + } + if (DBG) Log.i(TAG, "initializeSubscriptions:+"); + + // Before updating the the active subscription list check + // if tab updating is needed as the list is changing. + List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList(); + + // Update to the active subscription list + mActiveSubInfos.clear(); + if (sil != null) { + mActiveSubInfos.addAll(sil); + } + + if (DBG) Log.i(TAG, "initializeSubscriptions:-"); + } + + private boolean hasActiveSubscriptions() { + return mActiveSubInfos.size() > 0; } @Override |