AttachmentInfoDialog.java revision 9d9b481a85df540f8f338d28919802828a387efe
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_POLICY) != 0) {
59            bodyText = res.getString(R.string.attachment_info_policy);
60        } else if ((denyFlags & AttachmentInfo.DENY_NOINTENT) != 0) {
61            bodyText = res.getString(R.string.attachment_info_no_intent);
62        } else if ((denyFlags & AttachmentInfo.DENY_NOSIDELOAD) != 0) {
63            bodyText = res.getString(R.string.attachment_info_sideload_disabled);
64            actionText = res.getString(R.string.attachment_info_application_settings);
65            actionIntent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
66            actionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
67            actionIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
68        } else if ((denyFlags & AttachmentInfo.DENY_APKINSTALL) != 0) {
69            bodyText = res.getString(R.string.attachment_info_apk_install_disabled);
70        } else if ((denyFlags & AttachmentInfo.DENY_WIFIONLY) != 0) {
71            title = res.getString(R.string.attachment_info_dialog_wifi_title);
72            bodyText = res.getString(R.string.attachment_info_wifi_only);
73            actionText = res.getString(R.string.attachment_info_wifi_settings);
74            actionIntent = new Intent(Settings.ACTION_WIFI_SETTINGS);
75            actionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
76            actionIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
77        }
78        AttachmentInfoDialog dialog = new AttachmentInfoDialog();
79        Bundle args = new Bundle();
80        args.putString(BUNDLE_TITLE, title);
81        args.putString(BUNDLE_BODY_TEXT, bodyText);
82        args.putString(BUNDLE_ACTION_TEXT, actionText);
83        args.putParcelable(BUNDLE_ACTION_INTENT, actionIntent);
84        dialog.setArguments(args);
85        return dialog;
86    }
87
88    @Override
89    public Dialog onCreateDialog(Bundle savedInstanceState) {
90        Bundle args = getArguments();
91        Context context = getActivity();
92        String title = args.getString(BUNDLE_TITLE);
93        String infoText = args.getString(BUNDLE_BODY_TEXT);
94        String actionText = args.getString(BUNDLE_ACTION_TEXT);
95        final Intent actionIntent = args.getParcelable(BUNDLE_ACTION_INTENT);
96
97        OnClickListener onClickListener = new OnClickListener() {
98            @Override
99            public void onClick(DialogInterface dialog, int which) {
100                switch (which) {
101                    case DialogInterface.BUTTON_POSITIVE:
102                        startActivity(actionIntent);
103                        break;
104                    case DialogInterface.BUTTON_NEUTRAL:
105                        dialog.dismiss();
106                        break;
107                }
108            }
109        };
110
111        AlertDialog.Builder builder = new AlertDialog.Builder(context);
112        builder.setTitle(title);
113        builder.setMessage(infoText);
114        builder.setNeutralButton(R.string.okay_action, onClickListener);
115        if (actionText != null && actionIntent != null) {
116            builder.setPositiveButton(actionText, onClickListener);
117        }
118        return builder.show();
119    }
120}
121