1 /*
2  * Copyright (C) 2006 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.content.AsyncQueryHandler;
24 import android.content.ContentResolver;
25 import android.content.Intent;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.os.Handler;
30 import android.text.TextUtils;
31 import android.util.Log;
32 import android.view.Window;
33 import android.widget.Toast;
34 
35 import com.android.phone.PhoneGlobals;
36 import com.android.phone.R;
37 import com.android.phone.SubscriptionInfoHelper;
38 
39 /**
40  * Activity to let the user delete an FDN contact.
41  */
42 public class DeleteFdnContactScreen extends Activity {
43     private static final String LOG_TAG = PhoneGlobals.LOG_TAG;
44     private static final boolean DBG = false;
45 
46     private static final String INTENT_EXTRA_NAME = "name";
47     private static final String INTENT_EXTRA_NUMBER = "number";
48 
49     private static final int PIN2_REQUEST_CODE = 100;
50 
51     private SubscriptionInfoHelper mSubscriptionInfoHelper;
52 
53     private String mName;
54     private String mNumber;
55     private String mPin2;
56 
57     protected QueryHandler mQueryHandler;
58 
59     private Handler mHandler = new Handler();
60 
61     @Override
onCreate(Bundle icicle)62     protected void onCreate(Bundle icicle) {
63         super.onCreate(icicle);
64 
65         resolveIntent();
66 
67         // Starts PIN2 authentication only for the first time.
68         if (icicle == null) authenticatePin2();
69 
70         getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
71         setContentView(R.layout.delete_fdn_contact_screen);
72     }
73 
74     @Override
onActivityResult(int requestCode, int resultCode, Intent intent)75     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
76         if (DBG) log("onActivityResult");
77 
78         switch (requestCode) {
79             case PIN2_REQUEST_CODE:
80                 Bundle extras = (intent != null) ? intent.getExtras() : null;
81                 if (extras != null) {
82                     mPin2 = extras.getString("pin2");
83                     showStatus(getResources().getText(
84                             R.string.deleting_fdn_contact));
85                     deleteContact();
86                 } else {
87                     // if they cancelled, then we just cancel too.
88                     if (DBG) log("onActivityResult: CANCELLED");
89                     displayProgress(false);
90                     finish();
91                 }
92                 break;
93         }
94     }
95 
resolveIntent()96     private void resolveIntent() {
97         Intent intent = getIntent();
98 
99         mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, intent);
100 
101         mName =  intent.getStringExtra(INTENT_EXTRA_NAME);
102         mNumber =  intent.getStringExtra(INTENT_EXTRA_NUMBER);
103 
104         if (TextUtils.isEmpty(mNumber)) {
105             finish();
106         }
107     }
108 
deleteContact()109     private void deleteContact() {
110         StringBuilder buf = new StringBuilder();
111         if (TextUtils.isEmpty(mName)) {
112             buf.append("number='");
113         } else {
114             buf.append("tag='");
115             buf.append(mName);
116             buf.append("' AND number='");
117         }
118         buf.append(mNumber);
119         buf.append("' AND pin2='");
120         buf.append(mPin2);
121         buf.append("'");
122 
123         Uri uri = FdnList.getContentUri(mSubscriptionInfoHelper);
124 
125         mQueryHandler = new QueryHandler(getContentResolver());
126         mQueryHandler.startDelete(0, null, uri, buf.toString(), null);
127         displayProgress(true);
128     }
129 
authenticatePin2()130     private void authenticatePin2() {
131         Intent intent = new Intent();
132         intent.setClass(this, GetPin2Screen.class);
133         intent.setData(FdnList.getContentUri(mSubscriptionInfoHelper));
134         startActivityForResult(intent, PIN2_REQUEST_CODE);
135     }
136 
displayProgress(boolean flag)137     private void displayProgress(boolean flag) {
138         getWindow().setFeatureInt(
139                 Window.FEATURE_INDETERMINATE_PROGRESS,
140                 flag ? PROGRESS_VISIBILITY_ON : PROGRESS_VISIBILITY_OFF);
141     }
142 
143     // Replace the status field with a toast to make things appear similar
144     // to the rest of the settings.  Removed the useless status field.
showStatus(CharSequence statusMsg)145     private void showStatus(CharSequence statusMsg) {
146         if (statusMsg != null) {
147             Toast.makeText(this, statusMsg, Toast.LENGTH_SHORT)
148             .show();
149         }
150     }
151 
handleResult(boolean success)152     private void handleResult(boolean success) {
153         if (success) {
154             if (DBG) log("handleResult: success!");
155             showStatus(getResources().getText(R.string.fdn_contact_deleted));
156         } else {
157             if (DBG) log("handleResult: failed!");
158             showStatus(getResources().getText(R.string.pin2_invalid));
159         }
160 
161         mHandler.postDelayed(new Runnable() {
162             @Override
163             public void run() {
164                 finish();
165             }
166         }, 2000);
167 
168     }
169 
170     private class QueryHandler extends AsyncQueryHandler {
QueryHandler(ContentResolver cr)171         public QueryHandler(ContentResolver cr) {
172             super(cr);
173         }
174 
175         @Override
onQueryComplete(int token, Object cookie, Cursor c)176         protected void onQueryComplete(int token, Object cookie, Cursor c) {
177         }
178 
179         @Override
onInsertComplete(int token, Object cookie, Uri uri)180         protected void onInsertComplete(int token, Object cookie, Uri uri) {
181         }
182 
183         @Override
onUpdateComplete(int token, Object cookie, int result)184         protected void onUpdateComplete(int token, Object cookie, int result) {
185         }
186 
187         @Override
onDeleteComplete(int token, Object cookie, int result)188         protected void onDeleteComplete(int token, Object cookie, int result) {
189             if (DBG) log("onDeleteComplete");
190             displayProgress(false);
191             handleResult(result > 0);
192         }
193 
194     }
195 
log(String msg)196     private void log(String msg) {
197         Log.d(LOG_TAG, "[DeleteFdnContact] " + msg);
198     }
199 }
200