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