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
17package com.android.providers.downloads;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.content.ContentValues;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.database.Cursor;
26import android.net.Uri;
27import android.provider.Downloads;
28import android.text.format.Formatter;
29import android.util.Log;
30
31import java.util.LinkedList;
32import java.util.Queue;
33
34/**
35 * Activity to show dialogs to the user when a download exceeds a limit on download sizes for
36 * mobile networks.  This activity gets started by the background download service when a download's
37 * size is discovered to be exceeded one of these thresholds.
38 */
39public class SizeLimitActivity extends Activity
40        implements DialogInterface.OnCancelListener, DialogInterface.OnClickListener {
41    private Dialog mDialog;
42    private Queue<Intent> mDownloadsToShow = new LinkedList<Intent>();
43    private Uri mCurrentUri;
44    private Intent mCurrentIntent;
45
46    @Override
47    protected void onNewIntent(Intent intent) {
48        super.onNewIntent(intent);
49        setIntent(intent);
50    }
51
52    @Override
53    protected void onResume() {
54        super.onResume();
55        Intent intent = getIntent();
56        if (intent != null) {
57            mDownloadsToShow.add(intent);
58            setIntent(null);
59            showNextDialog();
60        }
61        if (mDialog != null && !mDialog.isShowing()) {
62            mDialog.show();
63        }
64    }
65
66    private void showNextDialog() {
67        if (mDialog != null) {
68            return;
69        }
70
71        if (mDownloadsToShow.isEmpty()) {
72            finish();
73            return;
74        }
75
76        mCurrentIntent = mDownloadsToShow.poll();
77        mCurrentUri = mCurrentIntent.getData();
78        Cursor cursor = getContentResolver().query(mCurrentUri, null, null, null, null);
79        try {
80            if (!cursor.moveToFirst()) {
81                Log.e(Constants.TAG, "Empty cursor for URI " + mCurrentUri);
82                dialogClosed();
83                return;
84            }
85            showDialog(cursor);
86        } finally {
87            cursor.close();
88        }
89    }
90
91    private void showDialog(Cursor cursor) {
92        int size = cursor.getInt(cursor.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TOTAL_BYTES));
93        String sizeString = Formatter.formatFileSize(this, size);
94        String queueText = getString(R.string.button_queue_for_wifi);
95        boolean isWifiRequired =
96            mCurrentIntent.getExtras().getBoolean(DownloadInfo.EXTRA_IS_WIFI_REQUIRED);
97
98        AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_DARK);
99        if (isWifiRequired) {
100            builder.setTitle(R.string.wifi_required_title)
101                    .setMessage(getString(R.string.wifi_required_body, sizeString, queueText))
102                    .setPositiveButton(R.string.button_queue_for_wifi, this)
103                    .setNegativeButton(R.string.button_cancel_download, this);
104        } else {
105            builder.setTitle(R.string.wifi_recommended_title)
106                    .setMessage(getString(R.string.wifi_recommended_body, sizeString, queueText))
107                    .setPositiveButton(R.string.button_start_now, this)
108                    .setNegativeButton(R.string.button_queue_for_wifi, this);
109        }
110        mDialog = builder.setOnCancelListener(this).show();
111    }
112
113    @Override
114    public void onCancel(DialogInterface dialog) {
115        dialogClosed();
116    }
117
118    private void dialogClosed() {
119        mDialog = null;
120        mCurrentUri = null;
121        showNextDialog();
122    }
123
124    @Override
125    public void onClick(DialogInterface dialog, int which) {
126        boolean isRequired =
127                mCurrentIntent.getExtras().getBoolean(DownloadInfo.EXTRA_IS_WIFI_REQUIRED);
128        if (isRequired && which == AlertDialog.BUTTON_NEGATIVE) {
129            getContentResolver().delete(mCurrentUri, null, null);
130        } else if (!isRequired && which == AlertDialog.BUTTON_POSITIVE) {
131            ContentValues values = new ContentValues();
132            values.put(Downloads.Impl.COLUMN_BYPASS_RECOMMENDED_SIZE_LIMIT, true);
133            getContentResolver().update(mCurrentUri, values , null, null);
134        }
135        dialogClosed();
136    }
137}
138