summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/DataUsageAppDetail.java
blob: c7c89ee90eec8f65e58ba1e2d61637bfc16cefa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings;

import static android.net.NetworkPolicyManager.POLICY_NONE;
import static android.net.NetworkPolicyManager.POLICY_REJECT_PAID_BACKGROUND;
import static android.net.TrafficStats.TEMPLATE_MOBILE_ALL;
import static com.android.settings.DataUsageSummary.getHistoryBounds;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.INetworkPolicyManager;
import android.net.INetworkStatsService;
import android.net.NetworkStatsHistory;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.text.format.DateUtils;
import android.text.format.Formatter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.settings.widget.DataUsageChartView;
import com.android.settings.widget.DataUsageChartView.DataUsageChartListener;

public class DataUsageAppDetail extends Fragment {
    private static final String TAG = "DataUsage";
    private static final boolean LOGD = true;

    private int mUid;

    private INetworkStatsService mStatsService;
    private INetworkPolicyManager mPolicyService;

    private CheckBoxPreference mRestrictBackground;
    private View mRestrictBackgroundView;

    private FrameLayout mChartContainer;
    private TextView mTitle;
    private TextView mText1;
    private Button mAppSettings;
    private LinearLayout mSwitches;

    private DataUsageChartView mChart;

    private int mUidPolicy;
    private NetworkStatsHistory mHistory;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mStatsService = INetworkStatsService.Stub.asInterface(
                ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
        mPolicyService = INetworkPolicyManager.Stub.asInterface(
                ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        final Context context = inflater.getContext();
        final View view = inflater.inflate(R.layout.data_usage_detail, container, false);

        mChartContainer = (FrameLayout) view.findViewById(R.id.chart_container);
        mTitle = (TextView) view.findViewById(android.R.id.title);
        mText1 = (TextView) view.findViewById(android.R.id.text1);
        mAppSettings = (Button) view.findViewById(R.id.data_usage_app_settings);
        mSwitches = (LinearLayout) view.findViewById(R.id.switches);

        mRestrictBackground = new CheckBoxPreference(context);
        mRestrictBackground.setTitle(R.string.data_usage_app_restrict_background);
        mRestrictBackground.setSummary(R.string.data_usage_app_restrict_background_summary);

        // kick refresh once to force-create views
        refreshPreferenceViews();

        mSwitches.addView(mRestrictBackgroundView);
        mRestrictBackgroundView.setOnClickListener(mRestrictBackgroundListener);

        mAppSettings.setOnClickListener(mAppSettingsListener);

        mChart = new DataUsageChartView(context);
        mChartContainer.addView(mChart);

        mChart.setListener(mChartListener);
        mChart.setChartColor(Color.parseColor("#d88d3a"), Color.parseColor("#c0ba7f3e"),
                Color.parseColor("#88566abc"));

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        final Context context = getActivity();

        mUid = getArguments().getInt(Intent.EXTRA_UID);
        mTitle.setText(context.getPackageManager().getNameForUid(mUid));

        updateBody();
    }

    private void updateBody() {
        try {
            // load stats for current uid and template
            // TODO: read template from extras
            mUidPolicy = mPolicyService.getUidPolicy(mUid);
            mHistory = mStatsService.getHistoryForUid(mUid, TEMPLATE_MOBILE_ALL);
        } catch (RemoteException e) {
            // since we can't do much without policy or history, and we don't
            // want to leave with half-baked UI, we bail hard.
            throw new RuntimeException("problem reading network stats", e);
        }

        // bind chart to historical stats
        mChart.bindNetworkStats(mHistory);

        // show entire history known
        final long[] bounds = getHistoryBounds(mHistory);
        mChart.setVisibleRange(bounds[0], bounds[1] + DateUtils.WEEK_IN_MILLIS, bounds[1]);
        updateDetailData();

        // update policy checkbox
        final boolean restrictBackground = (mUidPolicy & POLICY_REJECT_PAID_BACKGROUND) != 0;
        mRestrictBackground.setChecked(restrictBackground);

        // kick preference views so they rebind from changes above
        refreshPreferenceViews();
    }

    private void updateDetailData() {
        if (LOGD) Log.d(TAG, "updateDetailData()");

        final Context context = mChart.getContext();
        final long[] range = mChart.getInspectRange();
        final long[] total = mHistory.getTotalData(range[0], range[1], null);
        final long totalCombined = total[0] + total[1];
        mText1.setText(Formatter.formatFileSize(context, totalCombined));
    }

    /**
     * Force rebind of hijacked {@link Preference} views.
     */
    private void refreshPreferenceViews() {
        mRestrictBackgroundView = mRestrictBackground.getView(mRestrictBackgroundView, mSwitches);
    }

    private DataUsageChartListener mChartListener = new DataUsageChartListener() {
        /** {@inheritDoc} */
        public void onInspectRangeChanged() {
            if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
            updateDetailData();
        }

        /** {@inheritDoc} */
        public void onWarningChanged() {
            // ignored
        }

        /** {@inheritDoc} */
        public void onLimitChanged() {
            // ignored
        }
    };

    private OnClickListener mAppSettingsListener = new OnClickListener() {
        /** {@inheritDoc} */
        public void onClick(View v) {
            // TODO: target torwards entire UID instead of just first package
            final PackageManager pm = getActivity().getPackageManager();
            final String packageName = pm.getPackagesForUid(mUid)[0];

            final Intent intent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
            intent.setPackage(packageName);
            startActivity(intent);
        }
    };

    private OnClickListener mRestrictBackgroundListener = new OnClickListener() {
        /** {@inheritDoc} */
        public void onClick(View v) {
            final boolean restrictBackground = !mRestrictBackground.isChecked();
            mRestrictBackground.setChecked(restrictBackground);
            refreshPreferenceViews();

            try {
                mPolicyService.setUidPolicy(
                        mUid, restrictBackground ? POLICY_REJECT_PAID_BACKGROUND : POLICY_NONE);
            } catch (RemoteException e) {
                throw new RuntimeException("unable to save policy", e);
            }
        }
    };

}