• 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         getWindow().addSystemFlags(
75                 android.view.WindowManager.LayoutParams
76                         .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
77     }
78 
authenticatePin2()79     protected void authenticatePin2() {
80         Intent intent = new Intent();
81         intent.setClass(this, GetPin2Screen.class);
82         intent.setData(FdnList.getContentUri(mSubscriptionInfoHelper));
83         startActivityForResult(intent, PIN2_REQUEST_CODE);
84     }
85 
displayProgress(boolean flag)86     protected void displayProgress(boolean flag) {
87         getWindow().setFeatureInt(
88                 Window.FEATURE_INDETERMINATE_PROGRESS,
89                 flag ? PROGRESS_VISIBILITY_ON : PROGRESS_VISIBILITY_OFF);
90     }
91 
handleResult(boolean success)92     protected void handleResult(boolean success) {
93     }
94 
handleResult(boolean success, boolean invalidNumber)95     protected void handleResult(boolean success, boolean invalidNumber) {
96     }
97 
log(String msg)98     protected void log(String msg) {
99         Log.d(LOG_TAG, getClass().getSimpleName() + " : " + msg);
100     }
101 
102     // Add method to check if Pin2 supplied is correct.
processPin2(String pin)103     protected void processPin2(String pin) {
104         Message onComplete = mFDNHandler
105                 .obtainMessage(EVENT_PIN2_ENTRY_COMPLETE);
106         mPhone.getIccCard().supplyPin2(pin, onComplete);
107     }
108 
resolveIntent()109     protected void resolveIntent() {
110         Intent intent = getIntent();
111 
112         mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, intent);
113         mPhone = mSubscriptionInfoHelper.getPhone();
114 
115         mName =  intent.getStringExtra(INTENT_EXTRA_NAME);
116         mNumber =  intent.getStringExtra(INTENT_EXTRA_NUMBER);
117     }
118 
119     /**
120      * Removed the status field, with preference to displaying a toast
121      * to match the rest of settings UI.
122      */
showStatus(CharSequence statusMsg)123     protected void showStatus(CharSequence statusMsg) {
124         if (statusMsg != null) {
125             Toast.makeText(this, statusMsg, Toast.LENGTH_LONG).show();
126         }
127     }
128 
129     private Handler mFDNHandler = new Handler() {
130         @Override
131         public void handleMessage(Message msg) {
132             switch (msg.what) {
133                 case EVENT_PIN2_ENTRY_COMPLETE:
134                     AsyncResult ar = (AsyncResult) msg.obj;
135                     if (ar.exception != null) {
136                         // see if PUK2 is requested and alert the user accordingly.
137                         CommandException ce = (CommandException) ar.exception;
138                         if (ce.getCommandError() == CommandException.Error.SIM_PUK2) {
139                             // make sure we set the PUK2 state so that we can skip some
140                             // redundant behaviour.
141                             showPin2LockedDialog();
142                         } else {
143                             final int attemptsRemaining = msg.arg1;
144                             if (attemptsRemaining > 0) {
145                                 Toast.makeText(
146                                         BaseFdnContactScreen.this,
147                                         getString(R.string.pin2_invalid)
148                                                 + getString(R.string.pin2_attempts,
149                                                         attemptsRemaining), Toast.LENGTH_LONG)
150                                         .show();
151                                 finish();
152                             }
153                         }
154                     } else {
155                         pin2AuthenticationSucceed();
156                     }
157                     break;
158                 default:
159                     break;
160             }
161         }
162     };
163 
164     protected class QueryHandler extends AsyncQueryHandler {
QueryHandler(ContentResolver cr)165         protected QueryHandler(ContentResolver cr) {
166             super(cr);
167         }
168 
169         @Override
onInsertComplete(int token, Object cookie, Uri uri)170         protected void onInsertComplete(int token, Object cookie, Uri uri) {
171             if (DBG) log("onInsertComplete");
172             displayProgress(false);
173             handleResult(uri != null, false);
174         }
175 
176         @Override
onUpdateComplete(int token, Object cookie, int result)177         protected void onUpdateComplete(int token, Object cookie, int result) {
178             if (DBG) log("onUpdateComplete");
179             displayProgress(false);
180             handleResult(result > 0, false);
181         }
182 
183         @Override
onDeleteComplete(int token, Object cookie, int result)184         protected void onDeleteComplete(int token, Object cookie, int result) {
185             if (DBG) log("onDeleteComplete");
186             displayProgress(false);
187             handleResult(result > 0);
188         }
189     }
190 
showPin2LockedDialog()191     private void showPin2LockedDialog() {
192         final FragmentManager fragmentManager = getFragmentManager();
193         Pin2LockedDialogFragment dialogFragment = (Pin2LockedDialogFragment) fragmentManager
194                 .findFragmentByTag(Pin2LockedDialogFragment.TAG_PIN2_LOCKED_DIALOG);
195         if (dialogFragment == null) {
196             dialogFragment = new Pin2LockedDialogFragment();
197             Bundle args = new Bundle();
198             args.putInt(Pin2LockedDialogFragment.KEY_DIALOG_ID,
199                     Pin2LockedDialogFragment.DIALOG_ID_PUK2_REQUESTED_ON_PIN_ENTRY);
200             dialogFragment.setArguments(args);
201             dialogFragment.show(fragmentManager, Pin2LockedDialogFragment.TAG_PIN2_LOCKED_DIALOG);
202         } else {
203             FragmentTransaction transaction = fragmentManager.beginTransaction();
204             transaction.show(dialogFragment);
205             transaction.commitNow();
206         }
207     }
208 
209     @Override
onRequestPuk2(int id)210     public void onRequestPuk2(int id) {
211         finish();
212     }
213 }
214