CancelActivity.java revision 7cdbe5c0b11acb6128974f593b4ede86b01f95b2
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 */
16package com.android.contacts.vcard;
17
18import com.android.contacts.R;
19
20import android.app.Activity;
21import android.app.AlertDialog;
22import android.app.Dialog;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.DialogInterface;
26import android.content.Intent;
27import android.content.ServiceConnection;
28import android.net.Uri;
29import android.os.Bundle;
30import android.os.IBinder;
31import android.os.Message;
32import android.os.Messenger;
33import android.os.RemoteException;
34import android.util.Log;
35
36/**
37 * The Activity for canceling vCard import/export.
38 */
39public 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 message;
91            if (mType == VCardService.TYPE_IMPORT) {
92                message = getString(R.string.cancel_import_confirmation_message, mDisplayName);
93            } else {
94                message = getString(R.string.cancel_export_confirmation_message, mDisplayName);
95            }
96            final AlertDialog.Builder builder = new AlertDialog.Builder(this)
97                    .setMessage(message)
98                    .setPositiveButton(android.R.string.ok, new RequestCancelListener())
99                    .setOnCancelListener(mCancelListener)
100                    .setNegativeButton(android.R.string.cancel, mCancelListener);
101            return builder.create();
102        }
103        case R.id.dialog_cancel_failed:
104            final AlertDialog.Builder builder = new AlertDialog.Builder(this)
105                    .setTitle(R.string.cancel_vcard_import_or_export_failed)
106                    .setIconAttribute(android.R.attr.alertDialogIcon)
107                    .setMessage(getString(R.string.fail_reason_unknown))
108                    .setOnCancelListener(mCancelListener)
109                    .setPositiveButton(android.R.string.ok, mCancelListener);
110            return builder.create();
111        default:
112            Log.w(LOG_TAG, "Unknown dialog id: " + id);
113            break;
114        }
115        return super.onCreateDialog(id, bundle);
116    }
117
118    @Override
119    public void onServiceConnected(ComponentName name, IBinder binder) {
120        VCardService service = ((VCardService.MyBinder) binder).getService();
121
122        try {
123            final CancelRequest request = new CancelRequest(mJobId, mDisplayName);
124            service.handleCancelRequest(request, null);
125        } finally {
126            unbindService(this);
127        }
128
129        finish();
130    }
131
132    @Override
133    public void onServiceDisconnected(ComponentName name) {
134        // do nothing
135    }
136}
137