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.systemui.net;
18
19import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
20import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
21import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
22import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
23
24import android.app.Activity;
25import android.app.AlertDialog;
26import android.app.Dialog;
27import android.content.Context;
28import android.content.DialogInterface;
29import android.net.INetworkPolicyManager;
30import android.net.NetworkPolicy;
31import android.net.NetworkTemplate;
32import android.os.Bundle;
33import android.os.RemoteException;
34import android.os.ServiceManager;
35import android.util.Slog;
36import android.view.WindowManager;
37
38import com.android.systemui.R;
39
40/**
41 * Notify user that a {@link NetworkTemplate} is over its
42 * {@link NetworkPolicy#limitBytes}, giving them the choice of acknowledging or
43 * "snoozing" the limit.
44 */
45public class NetworkOverLimitActivity extends Activity {
46    private static final String TAG = "NetworkOverLimitActivity";
47
48    @Override
49    public void onCreate(Bundle icicle) {
50        super.onCreate(icicle);
51
52        final NetworkTemplate template = getIntent().getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
53        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
54        builder.setTitle(getLimitedDialogTitleForTemplate(template));
55        builder.setMessage(R.string.data_usage_disabled_dialog);
56
57        builder.setPositiveButton(android.R.string.ok, null);
58        builder.setNegativeButton(
59                R.string.data_usage_disabled_dialog_enable, new DialogInterface.OnClickListener() {
60                    public void onClick(DialogInterface dialog, int which) {
61                        snoozePolicy(template);
62                    }
63                });
64
65        final Dialog dialog = builder.create();
66        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
67            public void onDismiss(DialogInterface dialog) {
68                finish();
69            }
70        });
71
72        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
73        dialog.show();
74    }
75
76    private void snoozePolicy(NetworkTemplate template) {
77        final INetworkPolicyManager policyService = INetworkPolicyManager.Stub.asInterface(
78                ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
79        try {
80            policyService.snoozeLimit(template);
81        } catch (RemoteException e) {
82            Slog.w(TAG, "problem snoozing network policy", e);
83        }
84    }
85
86    private static int getLimitedDialogTitleForTemplate(NetworkTemplate template) {
87        switch (template.getMatchRule()) {
88            case MATCH_MOBILE_3G_LOWER:
89                return R.string.data_usage_disabled_dialog_3g_title;
90            case MATCH_MOBILE_4G:
91                return R.string.data_usage_disabled_dialog_4g_title;
92            case MATCH_MOBILE_ALL:
93                return R.string.data_usage_disabled_dialog_mobile_title;
94            default:
95                return R.string.data_usage_disabled_dialog_title;
96        }
97    }
98}
99