[Settings] To avoid use APIs in IccCard directly, calling APIs through TelephonyManager

Bug: 146983487
Test: manual
Change-Id: I4bf1341f972639829cd9fc64370018127460a65f
This commit is contained in:
zoey chen
2019-12-30 11:46:09 +08:00
parent 5d1bcb0408
commit 698e2ed95e

View File

@@ -24,11 +24,12 @@ import android.content.IntentFilter;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.os.AsyncResult; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
@@ -49,9 +50,6 @@ import android.widget.Toast;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreference;
import com.android.internal.telephony.CommandException;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;
import com.android.internal.telephony.TelephonyIntents; import com.android.internal.telephony.TelephonyIntents;
import com.android.settings.network.ProxySubscriptionManager; import com.android.settings.network.ProxySubscriptionManager;
@@ -112,7 +110,6 @@ public class IccLockSettings extends SettingsPreferenceFragment
private TabWidget mTabWidget; private TabWidget mTabWidget;
private ListView mListView; private ListView mListView;
private Phone mPhone;
private ProxySubscriptionManager mProxySubscriptionMgr; private ProxySubscriptionManager mProxySubscriptionMgr;
private EditPinPreference mPinDialog; private EditPinPreference mPinDialog;
@@ -121,24 +118,18 @@ public class IccLockSettings extends SettingsPreferenceFragment
private Resources mRes; private Resources mRes;
// For async handler to identify request type // For async handler to identify request type
private static final int MSG_ENABLE_ICC_PIN_COMPLETE = 100;
private static final int MSG_CHANGE_ICC_PIN_COMPLETE = 101;
private static final int MSG_SIM_STATE_CHANGED = 102; private static final int MSG_SIM_STATE_CHANGED = 102;
// @see android.widget.Toast$TN // @see android.widget.Toast$TN
private static final long LONG_DURATION_TIMEOUT = 7000; private static final long LONG_DURATION_TIMEOUT = 7000;
private int mSubId;
private TelephonyManager mTelephonyManager;
// For replies from IccCard interface // For replies from IccCard interface
private Handler mHandler = new Handler() { private Handler mHandler = new Handler() {
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
final AsyncResult ar = (AsyncResult) msg.obj;
switch (msg.what) { switch (msg.what) {
case MSG_ENABLE_ICC_PIN_COMPLETE:
iccLockChanged(ar.exception == null, msg.arg1, ar.exception);
break;
case MSG_CHANGE_ICC_PIN_COMPLETE:
iccPinChanged(ar.exception == null, msg.arg1);
break;
case MSG_SIM_STATE_CHANGED: case MSG_SIM_STATE_CHANGED:
updatePreferences(); updatePreferences();
break; break;
@@ -158,11 +149,12 @@ public class IccLockSettings extends SettingsPreferenceFragment
}; };
// For top-level settings screen to query // For top-level settings screen to query
static boolean isIccLockEnabled() { private boolean isIccLockEnabled() {
return PhoneFactory.getDefaultPhone().getIccCard().getIccLockEnabled(); mTelephonyManager = mTelephonyManager.createForSubscriptionId(mSubId);
return mTelephonyManager.isIccLockEnabled();
} }
static String getSummary(Context context) { private String getSummary(Context context) {
final Resources res = context.getResources(); final Resources res = context.getResources();
final String summary = isIccLockEnabled() final String summary = isIccLockEnabled()
? res.getString(R.string.sim_lock_on) ? res.getString(R.string.sim_lock_on)
@@ -184,6 +176,8 @@ public class IccLockSettings extends SettingsPreferenceFragment
mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(getContext()); mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(getContext());
mProxySubscriptionMgr.setLifecycle(getLifecycle()); mProxySubscriptionMgr.setLifecycle(getLifecycle());
mTelephonyManager = getContext().getSystemService(TelephonyManager.class);
addPreferencesFromResource(R.xml.sim_lock_settings); addPreferencesFromResource(R.xml.sim_lock_settings);
mPinDialog = (EditPinPreference) findPreference(PIN_DIALOG); mPinDialog = (EditPinPreference) findPreference(PIN_DIALOG);
@@ -251,15 +245,13 @@ public class IccLockSettings extends SettingsPreferenceFragment
: subInfo.getDisplayName()))); : subInfo.getDisplayName())));
} }
final SubscriptionInfo sir = getActiveSubscriptionInfoForSimSlotIndex(subInfoList, 0); final SubscriptionInfo sir = getActiveSubscriptionInfoForSimSlotIndex(subInfoList, 0);
mSubId = sir.getSubscriptionId();
mPhone = (sir == null) ? null : PhoneFactory.getPhone(sir.getSimSlotIndex());
if (savedInstanceState != null && savedInstanceState.containsKey(CURRENT_TAB)) { if (savedInstanceState != null && savedInstanceState.containsKey(CURRENT_TAB)) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString(CURRENT_TAB)); mTabHost.setCurrentTabByTag(savedInstanceState.getString(CURRENT_TAB));
} }
return view; return view;
} else { } else {
mPhone = PhoneFactory.getDefaultPhone();
return super.onCreateView(inflater, container, savedInstanceState); return super.onCreateView(inflater, container, savedInstanceState);
} }
} }
@@ -271,14 +263,20 @@ public class IccLockSettings extends SettingsPreferenceFragment
} }
private void updatePreferences() { private void updatePreferences() {
final List<SubscriptionInfo> subInfoList =
mProxySubscriptionMgr.getActiveSubscriptionsInfo();
final SubscriptionInfo sir = getActiveSubscriptionInfoForSimSlotIndex(subInfoList, 0);
mSubId = sir.getSubscriptionId();
if (mPinDialog != null) { if (mPinDialog != null) {
mPinDialog.setEnabled(mPhone != null); mPinDialog.setEnabled(sir != null);
} }
if (mPinToggle != null) { if (mPinToggle != null) {
mPinToggle.setEnabled(mPhone != null); mPinToggle.setEnabled(sir != null);
if (mPhone != null) { if (sir != null) {
mPinToggle.setChecked(mPhone.getIccCard().getIccLockEnabled()); mPinToggle.setChecked(isIccLockEnabled());
} }
} }
} }
@@ -462,29 +460,52 @@ public class IccLockSettings extends SettingsPreferenceFragment
private void tryChangeIccLockState() { private void tryChangeIccLockState() {
// Try to change icc lock. If it succeeds, toggle the lock state and // Try to change icc lock. If it succeeds, toggle the lock state and
// reset dialog state. Else inject error message and show dialog again. // reset dialog state. Else inject error message and show dialog again.
final Message callback = Message.obtain(mHandler, MSG_ENABLE_ICC_PIN_COMPLETE); new SetIccLockEnabled(mToState, mPin).execute();
mPhone.getIccCard().setIccLockEnabled(mToState, mPin, callback);
// Disable the setting till the response is received. // Disable the setting till the response is received.
mPinToggle.setEnabled(false); mPinToggle.setEnabled(false);
} }
private void iccLockChanged(boolean success, int attemptsRemaining, Throwable exception) { private class SetIccLockEnabled extends AsyncTask<Void, Void, Void> {
private final boolean mState;
private final String mPassword;
private int mAttemptsRemaining;
private SetIccLockEnabled(boolean state, String pin) {
mState = state;
mPassword = pin;
}
@Override
protected Void doInBackground(Void... params) {
mTelephonyManager = mTelephonyManager.createForSubscriptionId(mSubId);
mAttemptsRemaining = mTelephonyManager.setIccLockEnabled(mState, mPassword);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (mAttemptsRemaining == TelephonyManager.CHANGE_ICC_LOCK_SUCCESS) {
iccLockChanged(true, mAttemptsRemaining);
} else {
iccLockChanged(false, mAttemptsRemaining);
}
}
}
private void iccLockChanged(boolean success, int attemptsRemaining) {
Log.d(TAG, "iccLockChanged: success = " + success);
if (success) { if (success) {
mPinToggle.setChecked(mToState); mPinToggle.setChecked(mToState);
} else { } else {
if (exception instanceof CommandException) { if (attemptsRemaining >= 0) {
final CommandException.Error err = createCustomTextToast(getPinPasswordErrorMessage(attemptsRemaining));
((CommandException) exception).getCommandError(); } else {
if (err == CommandException.Error.PASSWORD_INCORRECT) { if (mToState) {
createCustomTextToast(getPinPasswordErrorMessage(attemptsRemaining)); Toast.makeText(getContext(), mRes.getString(
R.string.sim_pin_enable_failed), Toast.LENGTH_LONG).show();
} else { } else {
if (mToState) { Toast.makeText(getContext(), mRes.getString(
Toast.makeText(getContext(), mRes.getString R.string.sim_pin_disable_failed), Toast.LENGTH_LONG).show();
(R.string.sim_pin_enable_failed), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getContext(), mRes.getString
(R.string.sim_pin_disable_failed), Toast.LENGTH_LONG).show();
}
} }
} }
} }
@@ -540,21 +561,46 @@ public class IccLockSettings extends SettingsPreferenceFragment
} }
private void iccPinChanged(boolean success, int attemptsRemaining) { private void iccPinChanged(boolean success, int attemptsRemaining) {
Log.d(TAG, "iccPinChanged: success = " + success);
if (!success) { if (!success) {
createCustomTextToast(getPinPasswordErrorMessage(attemptsRemaining)); createCustomTextToast(getPinPasswordErrorMessage(attemptsRemaining));
} else { } else {
Toast.makeText(getContext(), mRes.getString(R.string.sim_change_succeeded), Toast.makeText(getContext(), mRes.getString(R.string.sim_change_succeeded),
Toast.LENGTH_SHORT) Toast.LENGTH_SHORT)
.show(); .show();
} }
resetDialogState(); resetDialogState();
} }
private void tryChangePin() { private void tryChangePin() {
final Message callback = Message.obtain(mHandler, MSG_CHANGE_ICC_PIN_COMPLETE); new ChangeIccLockPassword(mOldPin, mNewPin).execute();
mPhone.getIccCard().changeIccLockPassword(mOldPin, }
mNewPin, callback);
private class ChangeIccLockPassword extends AsyncTask<Void, Void, Void> {
private final String mOldPwd;
private final String mNewPwd;
private int mAttemptsRemaining;
private ChangeIccLockPassword(String oldPin, String newPin) {
mOldPwd = oldPin;
mNewPwd = newPin;
}
@Override
protected Void doInBackground(Void... params) {
mTelephonyManager = mTelephonyManager.createForSubscriptionId(mSubId);
mAttemptsRemaining = mTelephonyManager.changeIccLockPassword(mOldPwd, mNewPwd);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (mAttemptsRemaining == TelephonyManager.CHANGE_ICC_LOCK_SUCCESS) {
iccPinChanged(true, mAttemptsRemaining);
} else {
iccPinChanged(false, mAttemptsRemaining);
}
}
} }
private String getPinPasswordErrorMessage(int attemptsRemaining) { private String getPinPasswordErrorMessage(int attemptsRemaining) {
@@ -610,8 +656,6 @@ public class IccLockSettings extends SettingsPreferenceFragment
final SubscriptionInfo sir = getActiveSubscriptionInfoForSimSlotIndex( final SubscriptionInfo sir = getActiveSubscriptionInfoForSimSlotIndex(
mProxySubscriptionMgr.getActiveSubscriptionsInfo(), slotId); mProxySubscriptionMgr.getActiveSubscriptionsInfo(), slotId);
mPhone = (sir == null) ? null : PhoneFactory.getPhone(sir.getSimSlotIndex());
// The User has changed tab; update the body. // The User has changed tab; update the body.
updatePreferences(); updatePreferences();
} }