1 /* 2 * Copyright (C) 2010 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 package com.android.contacts.vcard; 17 18 import com.android.contacts.R; 19 20 import android.app.Activity; 21 import android.app.AlertDialog; 22 import android.app.Dialog; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.DialogInterface; 26 import android.content.Intent; 27 import android.content.ServiceConnection; 28 import android.net.Uri; 29 import android.os.Bundle; 30 import android.os.IBinder; 31 import android.os.Message; 32 import android.os.Messenger; 33 import android.os.RemoteException; 34 import android.util.Log; 35 36 /** 37 * The Activity for canceling vCard import/export. 38 */ 39 public class CancelActivity extends Activity implements ServiceConnection { 40 private final String LOG_TAG = "VCardCancel"; 41 42 /* package */ final static String JOB_ID = "job_id"; 43 /* package */ final static String DISPLAY_NAME = "display_name"; 44 45 /** 46 * Type of the process to be canceled. Only used for choosing appropriate title/message. 47 * Must be {@link VCardService#TYPE_IMPORT} or {@link VCardService#TYPE_EXPORT}. 48 */ 49 /* package */ final static String TYPE = "type"; 50 51 private class RequestCancelListener implements DialogInterface.OnClickListener { 52 @Override 53 public void onClick(DialogInterface dialog, int which) { 54 bindService(new Intent(CancelActivity.this, 55 VCardService.class), CancelActivity.this, Context.BIND_AUTO_CREATE); 56 } 57 } 58 59 private class CancelListener 60 implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener { 61 @Override 62 public void onClick(DialogInterface dialog, int which) { 63 finish(); 64 } 65 @Override 66 public void onCancel(DialogInterface dialog) { 67 finish(); 68 } 69 } 70 71 private final CancelListener mCancelListener = new CancelListener(); 72 private int mJobId; 73 private String mDisplayName; 74 private int mType; 75 76 @Override 77 public void onCreate(Bundle savedInstanceState) { 78 super.onCreate(savedInstanceState); 79 final Uri uri = getIntent().getData(); 80 mJobId = Integer.parseInt(uri.getQueryParameter(JOB_ID)); 81 mDisplayName = uri.getQueryParameter(DISPLAY_NAME); 82 mType = Integer.parseInt(uri.getQueryParameter(TYPE)); 83 showDialog(R.id.dialog_cancel_confirmation); 84 } 85 86 @Override 87 protected Dialog onCreateDialog(int id, Bundle bundle) { 88 switch (id) { 89 case R.id.dialog_cancel_confirmation: { 90 final String title; 91 final String message; 92 if (mType == VCardService.TYPE_IMPORT) { 93 title = getString(R.string.cancel_import_confirmation_title); 94 message = getString(R.string.cancel_import_confirmation_message, mDisplayName); 95 } else { 96 title = getString(R.string.cancel_export_confirmation_title); 97 message = getString(R.string.cancel_export_confirmation_message, mDisplayName); 98 } 99 final AlertDialog.Builder builder = new AlertDialog.Builder(this) 100 .setTitle(title) 101 .setMessage(message) 102 .setPositiveButton(android.R.string.ok, new RequestCancelListener()) 103 .setOnCancelListener(mCancelListener) 104 .setNegativeButton(android.R.string.cancel, mCancelListener); 105 return builder.create(); 106 } 107 case R.id.dialog_cancel_failed: 108 final AlertDialog.Builder builder = new AlertDialog.Builder(this) 109 .setTitle(R.string.cancel_vcard_import_or_export_failed) 110 .setIconAttribute(android.R.attr.alertDialogIcon) 111 .setMessage(getString(R.string.fail_reason_unknown)) 112 .setOnCancelListener(mCancelListener) 113 .setPositiveButton(android.R.string.ok, mCancelListener); 114 return builder.create(); 115 default: 116 Log.w(LOG_TAG, "Unknown dialog id: " + id); 117 break; 118 } 119 return super.onCreateDialog(id, bundle); 120 } 121 122 @Override 123 public void onServiceConnected(ComponentName name, IBinder binder) { 124 VCardService service = ((VCardService.MyBinder) binder).getService(); 125 126 try { 127 final CancelRequest request = new CancelRequest(mJobId, mDisplayName); 128 service.handleCancelRequest(request, null); 129 } finally { 130 unbindService(this); 131 } 132 133 finish(); 134 } 135 136 @Override 137 public void onServiceDisconnected(ComponentName name) { 138 // do nothing 139 } 140 } 141