Extend trampoline to show dialogs.
[android/platform/packages/providers/DownloadProvider.git] / ui / src / com / android / providers / downloads / ui / TrampolineActivity.java
1 /*
2  * Copyright (C) 2013 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.providers.downloads.ui;
18
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.app.DownloadManager;
24 import android.app.DownloadManager.Query;
25 import android.app.FragmentManager;
26 import android.content.ContentUris;
27 import android.content.Context;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.database.Cursor;
31 import android.os.Bundle;
32 import android.util.Log;
33 import android.widget.Toast;
34
35 import com.android.providers.downloads.Constants;
36 import com.android.providers.downloads.OpenHelper;
37
38 import libcore.io.IoUtils;
39
40 /**
41  * Intercept all download clicks to provide special behavior. For example,
42  * PackageInstaller really wants raw file paths.
43  */
44 public class TrampolineActivity extends Activity {
45     private static final String TAG_PAUSED = "paused";
46     private static final String TAG_FAILED = "failed";
47
48     private static final String KEY_ID = "id";
49     private static final String KEY_REASON = "reason";
50
51     @Override
52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54
55         final long id = ContentUris.parseId(getIntent().getData());
56
57         final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
58         dm.setAccessAllDownloads(true);
59
60         final int status;
61         final int reason;
62
63         final Cursor cursor = dm.query(new Query().setFilterById(id));
64         try {
65             if (cursor.moveToFirst()) {
66                 status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
67                 reason = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON));
68             } else {
69                 Toast.makeText(this, R.string.dialog_file_missing_body, Toast.LENGTH_SHORT).show();
70                 finish();
71                 return;
72             }
73         } finally {
74             IoUtils.closeQuietly(cursor);
75         }
76
77         Log.d(Constants.TAG, "Found " + id + " with status " + status + ", reason " + reason);
78         switch (status) {
79             case DownloadManager.STATUS_PENDING:
80             case DownloadManager.STATUS_RUNNING:
81                 sendRunningDownloadClickedBroadcast(id);
82                 finish();
83                 break;
84
85             case DownloadManager.STATUS_PAUSED:
86                 if (reason == DownloadManager.PAUSED_QUEUED_FOR_WIFI) {
87                     PausedDialogFragment.show(getFragmentManager(), id);
88                 } else {
89                     sendRunningDownloadClickedBroadcast(id);
90                     finish();
91                 }
92                 break;
93
94             case DownloadManager.STATUS_SUCCESSFUL:
95                 final Intent intent = OpenHelper.buildViewIntent(this, id);
96                 startActivity(intent);
97                 finish();
98                 break;
99
100             case DownloadManager.STATUS_FAILED:
101                 FailedDialogFragment.show(getFragmentManager(), id, reason);
102                 break;
103         }
104     }
105
106     private void sendRunningDownloadClickedBroadcast(long id) {
107         final Intent intent = new Intent(Constants.ACTION_LIST);
108         intent.setPackage(Constants.PROVIDER_PACKAGE_NAME);
109         intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, new long[] { id });
110         sendBroadcast(intent);
111     }
112
113     public static class PausedDialogFragment extends DialogFragment {
114         public static void show(FragmentManager fm, long id) {
115             final PausedDialogFragment dialog = new PausedDialogFragment();
116             final Bundle args = new Bundle();
117             args.putLong(KEY_ID, id);
118             dialog.setArguments(args);
119             dialog.show(fm, TAG_PAUSED);
120         }
121
122         @Override
123         public Dialog onCreateDialog(Bundle savedInstanceState) {
124             final Context context = getActivity();
125
126             final DownloadManager dm = (DownloadManager) context.getSystemService(
127                     Context.DOWNLOAD_SERVICE);
128             dm.setAccessAllDownloads(true);
129
130             final long id = getArguments().getLong(KEY_ID);
131
132             final AlertDialog.Builder builder = new AlertDialog.Builder(
133                     context, AlertDialog.THEME_HOLO_LIGHT);
134             builder.setTitle(R.string.dialog_title_queued_body);
135             builder.setMessage(R.string.dialog_queued_body);
136
137             builder.setPositiveButton(R.string.keep_queued_download, null);
138
139             builder.setNegativeButton(
140                     R.string.remove_download, new DialogInterface.OnClickListener() {
141                         @Override
142                         public void onClick(DialogInterface dialog, int which) {
143                             dm.remove(id);
144                         }
145                     });
146
147             return builder.create();
148         }
149
150         @Override
151         public void onDismiss(DialogInterface dialog) {
152             super.onDismiss(dialog);
153             getActivity().finish();
154         }
155     }
156
157     public static class FailedDialogFragment extends DialogFragment {
158         public static void show(FragmentManager fm, long id, int reason) {
159             final FailedDialogFragment dialog = new FailedDialogFragment();
160             final Bundle args = new Bundle();
161             args.putLong(KEY_ID, id);
162             args.putInt(KEY_REASON, reason);
163             dialog.setArguments(args);
164             dialog.show(fm, TAG_FAILED);
165         }
166
167         @Override
168         public Dialog onCreateDialog(Bundle savedInstanceState) {
169             final Context context = getActivity();
170
171             final DownloadManager dm = (DownloadManager) context.getSystemService(
172                     Context.DOWNLOAD_SERVICE);
173             dm.setAccessAllDownloads(true);
174
175             final long id = getArguments().getLong(KEY_ID);
176             final int reason = getArguments().getInt(KEY_REASON);
177
178             final AlertDialog.Builder builder = new AlertDialog.Builder(
179                     context, AlertDialog.THEME_HOLO_LIGHT);
180             builder.setTitle(R.string.dialog_title_not_available);
181
182             final String message;
183             switch (reason) {
184                 case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
185                     builder.setMessage(R.string.dialog_file_already_exists);
186                     break;
187                 case DownloadManager.ERROR_INSUFFICIENT_SPACE:
188                     builder.setMessage(R.string.dialog_insufficient_space_on_external);
189                     break;
190                 case DownloadManager.ERROR_DEVICE_NOT_FOUND:
191                     builder.setMessage(R.string.dialog_media_not_found);
192                     break;
193                 case DownloadManager.ERROR_CANNOT_RESUME:
194                     builder.setMessage(R.string.dialog_cannot_resume);
195                     break;
196                 default:
197                     builder.setMessage(R.string.dialog_failed_body);
198             }
199
200             builder.setNegativeButton(
201                     R.string.delete_download, new DialogInterface.OnClickListener() {
202                         @Override
203                         public void onClick(DialogInterface dialog, int which) {
204                             dm.remove(id);
205                         }
206                     });
207
208             builder.setPositiveButton(
209                     R.string.retry_download, new DialogInterface.OnClickListener() {
210                         @Override
211                         public void onClick(DialogInterface dialog, int which) {
212                             dm.restartDownload(id);
213                         }
214                     });
215
216             return builder.create();
217         }
218
219         @Override
220         public void onDismiss(DialogInterface dialog) {
221             super.onDismiss(dialog);
222             getActivity().finish();
223         }
224     }
225 }