1/*
2 * Copyright (C) 2012 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
17package com.android.providers.contacts.debug;
18
19import com.android.providers.contacts.R;
20
21import android.app.Activity;
22import android.content.Intent;
23import android.net.Uri;
24import android.os.AsyncTask;
25import android.os.Bundle;
26import android.util.Log;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.view.Window;
30import android.widget.Button;
31
32import java.io.IOException;
33
34/**
35 * Activity to export all app data files as a zip file on sdcard, and send it via email.
36 *
37 * Usage:
38 * adb shell am start -a com.android.providers.contacts.DUMP_DATABASE
39 */
40public class ContactsDumpActivity extends Activity implements OnClickListener {
41    private static String TAG = "ContactsDumpActivity";
42    private Button mConfirmButton;
43    private Button mCancelButton;
44    private Button mDeleteButton;
45
46    @Override
47    protected void onCreate(Bundle savedInstanceState) {
48        // Be sure to call the super class.
49        super.onCreate(savedInstanceState);
50
51        requestWindowFeature(Window.FEATURE_LEFT_ICON);
52
53        setContentView(R.layout.contact_dump_activity);
54
55        getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
56                android.R.drawable.ic_dialog_alert);
57
58        mConfirmButton = (Button) findViewById(R.id.confirm);
59        mCancelButton = (Button) findViewById(R.id.cancel);
60        mDeleteButton = (Button) findViewById(R.id.delete);
61        updateDeleteButton();
62    }
63
64    private void updateDeleteButton() {
65        mDeleteButton.setEnabled(DataExporter.dumpFileExists(this));
66    }
67
68    @Override
69    public void onClick(View v) {
70        switch (v.getId()) {
71            case R.id.confirm:
72                mConfirmButton.setEnabled(false);
73                mCancelButton.setEnabled(false);
74                new DumpDbTask().execute();
75                break;
76            case R.id.delete:
77                cleanup();
78                updateDeleteButton();
79                break;
80            case R.id.cancel:
81                finish();
82                break;
83        }
84    }
85
86    private void cleanup() {
87        DataExporter.removeDumpFiles(this);
88    }
89
90    private class DumpDbTask extends AsyncTask<Void, Void, Uri> {
91        /**
92         * Starts spinner while task is running.
93         */
94        @Override
95        protected void onPreExecute() {
96            setProgressBarIndeterminateVisibility(true);
97        }
98
99        @Override
100        protected Uri doInBackground(Void... params) {
101            try {
102                return DataExporter.exportData(getApplicationContext());
103            } catch (IOException e) {
104                Log.e(TAG, "Failed to export", e);
105                return null;
106            }
107        }
108
109        @Override
110        protected void onPostExecute(Uri uri) {
111            if (uri != null) {
112                emailFile(uri);
113            }
114        }
115    }
116
117    private void emailFile(Uri uri) {
118        Log.i(TAG, "Drafting email");
119        Intent intent = new Intent(Intent.ACTION_SEND);
120        intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.debug_dump_email_subject));
121        intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.debug_dump_email_body));
122        intent.setType(DataExporter.ZIP_MIME_TYPE);
123        intent.putExtra(Intent.EXTRA_STREAM, uri);
124        startActivityForResult(Intent.createChooser(intent,
125                getString(R.string.debug_dump_email_sender_picker)), 0);
126    }
127
128    @Override
129    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
130        updateDeleteButton();
131        mConfirmButton.setEnabled(true);
132        mCancelButton.setEnabled(true);
133    }
134}
135