AttachmentInfoDialog.java revision 3f60e9312b1bf1324dd0e343b07cb69c9f8d31fd
1/*
2 * Copyright (C) 2011 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.email.activity;
18
19import com.android.email.AttachmentInfo;
20import com.android.email.R;
21
22import android.app.AlertDialog;
23import android.app.Dialog;
24import android.app.DialogFragment;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.DialogInterface.OnClickListener;
28import android.content.Intent;
29import android.content.res.Resources;
30import android.os.Bundle;
31import android.provider.Settings;
32
33/**
34 * "Info" dialog box
35 */
36public class AttachmentInfoDialog extends DialogFragment {
37    private static final String BUNDLE_TITLE         = "title";
38    private static final String BUNDLE_BODY_TEXT     = "body_text";
39    private static final String BUNDLE_ACTION_TEXT   = "action_text";
40    private static final String BUNDLE_ACTION_INTENT = "action_intent";
41
42    /**
43     * Returns a new dialog instance
44     */
45    public static AttachmentInfoDialog newInstance(Context context, int denyFlags) {
46        Resources res = context.getResources();
47        String title = res.getString(R.string.attachment_info_dialog_default_title);
48        String bodyText = res.getString(R.string.attachment_info_unknown);
49        String actionText = null;
50        Intent actionIntent = null;
51
52        // NOTE: Order here matters. There can be multiple reasons for denying an attachment,
53        // so, we want to show the most important ones first (i.e. it's pointless to tell the
54        // user to connect to wi-fi to download a 30mb attachment that is suspeceted of being
55        // malware).
56        if ((denyFlags & AttachmentInfo.DENY_MALWARE) != 0) {
57            bodyText = res.getString(R.string.attachment_info_malware);
58        } else if ((denyFlags & AttachmentInfo.DENY_NOINTENT) != 0) {
59            bodyText = res.getString(R.string.attachment_info_no_intent);
60        } else if ((denyFlags & AttachmentInfo.DENY_NOSIDELOAD) != 0) {
61            bodyText = res.getString(R.string.attachment_info_sideload_disabled);
62            actionText = res.getString(R.string.attachment_info_application_settings);
63            actionIntent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
64            actionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
65            actionIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
66        } else if ((denyFlags & AttachmentInfo.DENY_APKINSTALL) != 0) {
67            bodyText = res.getString(R.string.attachment_info_apk_install_disabled);
68        } else if ((denyFlags & AttachmentInfo.DENY_WIFIONLY) != 0) {
69            title = res.getString(R.string.attachment_info_dialog_wifi_title);
70            bodyText = res.getString(R.string.attachment_info_wifi_only);
71            actionText = res.getString(R.string.attachment_info_wifi_settings);
72            actionIntent = new Intent(Settings.ACTION_WIFI_SETTINGS);
73            actionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74            actionIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
75        }
76        AttachmentInfoDialog dialog = new AttachmentInfoDialog();
77        Bundle args = new Bundle();
78        args.putString(BUNDLE_TITLE, title);
79        args.putString(BUNDLE_BODY_TEXT, bodyText);
80        args.putString(BUNDLE_ACTION_TEXT, actionText);
81        args.putParcelable(BUNDLE_ACTION_INTENT, actionIntent);
82        dialog.setArguments(args);
83        return dialog;
84    }
85
86    @Override
87    public Dialog onCreateDialog(Bundle savedInstanceState) {
88        Bundle args = getArguments();
89        Context context = getActivity();
90        String title = args.getString(BUNDLE_TITLE);
91        String infoText = args.getString(BUNDLE_BODY_TEXT);
92        String actionText = args.getString(BUNDLE_ACTION_TEXT);
93        final Intent actionIntent = args.getParcelable(BUNDLE_ACTION_INTENT);
94
95        OnClickListener onClickListener = new OnClickListener() {
96            @Override
97            public void onClick(DialogInterface dialog, int which) {
98                switch (which) {
99                    case DialogInterface.BUTTON_POSITIVE:
100                        startActivity(actionIntent);
101                        break;
102                    case DialogInterface.BUTTON_NEUTRAL:
103                        dialog.dismiss();
104                        break;
105                }
106            }
107        };
108
109        AlertDialog.Builder builder = new AlertDialog.Builder(context);
110        builder.setTitle(title);
111        builder.setMessage(infoText);
112        builder.setNeutralButton(R.string.okay_action, onClickListener);
113        if (actionText != null && actionIntent != null) {
114            builder.setPositiveButton(actionText, onClickListener);
115        }
116        return builder.show();
117    }
118}
119