• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.phone.settings.fdn;
18 
19 import static android.view.Window.PROGRESS_VISIBILITY_OFF;
20 import static android.view.Window.PROGRESS_VISIBILITY_ON;
21 
22 import android.app.Activity;
23 import android.app.FragmentManager;
24 import android.app.FragmentTransaction;
25 import android.content.AsyncQueryHandler;
26 import android.content.ContentResolver;
27 import android.content.Intent;
28 import android.net.Uri;
29 import android.os.AsyncResult;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.os.Message;
33 import android.util.Log;
34 import android.view.Window;
35 import android.widget.Toast;
36 
37 import com.android.internal.telephony.CommandException;
38 import com.android.internal.telephony.Phone;
39 import com.android.phone.PhoneGlobals;
40 import com.android.phone.R;
41 import com.android.phone.SubscriptionInfoHelper;
42 
43 /**
44  * Base activity for FDN contact screen.
45  */
46 public abstract class BaseFdnContactScreen extends Activity
47         implements Pin2LockedDialogFragment.Listener {
48     protected static final String LOG_TAG = PhoneGlobals.LOG_TAG;
49     protected static final boolean DBG = false;
50 
51     protected static final int EVENT_PIN2_ENTRY_COMPLETE = 10;
52     protected static final int PIN2_REQUEST_CODE = 100;
53 
54     protected static final String INTENT_EXTRA_NAME = "name";
55     protected static final String INTENT_EXTRA_NUMBER = "number";
56 
57     protected String mName;
58     protected String mNumber;
59     protected String mPin2;
60 
61     protected SubscriptionInfoHelper mSubscriptionInfoHelper;
62     protected BaseFdnContactScreen.QueryHandler mQueryHandler;
63 
64     protected Handler mHandler = new Handler();
65     protected Phone mPhone;
66 
pin2AuthenticationSucceed()67     protected abstract void pin2AuthenticationSucceed();
68 
69     @Override
onCreate(Bundle savedInstanceState)70     protected void onCreate(Bundle savedInstanceState) {
71         super.onCreate(savedInstanceState);
72         resolveIntent();
73         getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
74     }
75 
authenticatePin2()76     protected void authenticatePin2() {
77         Intent intent = new Intent();
78         intent.setClass(this, GetPin2Screen.class);
79         intent.setData(FdnList.getContentUri(mSubscriptionInfoHelper));
80         startActivityForResult(intent, PIN2_REQUEST_CODE);
81     }
82 
displayProgress(boolean flag)83     protected void displayProgress(boolean flag) {
84         getWindow().setFeatureInt(
85                 Window.FEATURE_INDETERMINATE_PROGRESS,
86                 flag ? PROGRESS_VISIBILITY_ON : PROGRESS_VISIBILITY_OFF);
87     }
88 
handleResult(boolean success)89     protected void handleResult(boolean success) {
90     }
91 
handleResult(boolean success, boolean invalidNumber)92     protected void handleResult(boolean success, boolean invalidNumber) {
93     }
94 
log(String msg)95     protected void log(String msg) {
96         Log.d(LOG_TAG, getClass().getSimpleName() + " : " + msg);
97     }
98 
99     // Add method to check if Pin2 supplied is correct.
processPin2(String pin)100     protected void processPin2(String pin) {
101         Message onComplete = mFDNHandler
102                 .obtainMessage(EVENT_PIN2_ENTRY_COMPLETE);
103         mPhone.getIccCard().supplyPin2(pin, onComplete);
104     }
105 
resolveIntent()106     protected void resolveIntent() {
107         Intent intent = getIntent();
108 
109         mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, intent);
110         mPhone = mSubscriptionInfoHelper.getPhone();
111 
112         mName =  intent.getStringExtra(INTENT_EXTRA_NAME);
113         mNumber =  intent.getStringExtra(INTENT_EXTRA_NUMBER);
114     }
115 
116     /**
117      * Removed the status field, with preference to displaying a toast
118      * to match the rest of settings UI.
119      */
showStatus(CharSequence statusMsg)120     protected void showStatus(CharSequence statusMsg) {
121         if (statusMsg != null) {
122             Toast.makeText(this, statusMsg, Toast.LENGTH_LONG).show();
123         }
124     }
125 
126     private Handler mFDNHandler = new Handler() {
127         @Override
128         public void handleMessage(Message msg) {
129             switch (msg.what) {
130                 case EVENT_PIN2_ENTRY_COMPLETE:
131                     AsyncResult ar = (AsyncResult) msg.obj;
132                     if (ar.exception != null) {
133                         // see if PUK2 is requested and alert the user accordingly.
134                         CommandException ce = (CommandException) ar.exception;
135                         if (ce.getCommandError() == CommandException.Error.SIM_PUK2) {
136                             // make sure we set the PUK2 state so that we can skip some
137                             // redundant behaviour.
138                             showPin2LockedDialog();
139                         } else {
140                             final int attemptsRemaining = msg.arg1;
141                             if (attemptsRemaining > 0) {
142                                 Toast.makeText(
143                                         BaseFdnContactScreen.this,
144                                         getString(R.string.pin2_invalid)
145                                                 + getString(R.string.pin2_attempts,
146                                                         attemptsRemaining), Toast.LENGTH_LONG)
147                                         .show();
148                                 finish();
149                             }
150                         }
151                     } else {
152                         pin2AuthenticationSucceed();
153                     }
154                     break;
155                 default:
156                     break;
157             }
158         }
159     };
160 
161     protected class QueryHandler extends AsyncQueryHandler {
QueryHandler(ContentResolver cr)162         protected QueryHandler(ContentResolver cr) {
163             super(cr);
164         }
165 
166         @Override
onInsertComplete(int token, Object cookie, Uri uri)167         protected void onInsertComplete(int token, Object cookie, Uri uri) {
168             if (DBG) log("onInsertComplete");
169             displayProgress(false);
170             handleResult(uri != null, false);
171         }
172 
173         @Override
onUpdateComplete(int token, Object cookie, int result)174         protected void onUpdateComplete(int token, Object cookie, int result) {
175             if (DBG) log("onUpdateComplete");
176             displayProgress(false);
177             handleResult(result > 0, false);
178         }
179 
180         @Override
onDeleteComplete(int token, Object cookie, int result)181         protected void onDeleteComplete(int token, Object cookie, int result) {
182             if (DBG) log("onDeleteComplete");
183             displayProgress(false);
184             handleResult(result > 0);
185         }
186     }
187 
showPin2LockedDialog()188     private void showPin2LockedDialog() {
189         final FragmentManager fragmentManager = getFragmentManager();
190         Pin2LockedDialogFragment dialogFragment = (Pin2LockedDialogFragment) fragmentManager
191                 .findFragmentByTag(Pin2LockedDialogFragment.TAG_PIN2_LOCKED_DIALOG);
192         if (dialogFragment == null) {
193             dialogFragment = new Pin2LockedDialogFragment();
194             Bundle args = new Bundle();
195             args.putInt(Pin2LockedDialogFragment.KEY_DIALOG_ID,
196                     Pin2LockedDialogFragment.DIALOG_ID_PUK2_REQUESTED_ON_PIN_ENTRY);
197             dialogFragment.setArguments(args);
198             dialogFragment.show(fragmentManager, Pin2LockedDialogFragment.TAG_PIN2_LOCKED_DIALOG);
199         } else {
200             FragmentTransaction transaction = fragmentManager.beginTransaction();
201             transaction.show(dialogFragment);
202             transaction.commitNow();
203         }
204     }
205 
206     @Override
onRequestPuk2(int id)207     public void onRequestPuk2(int id) {
208         finish();
209     }
210 }
211