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