1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.settings.datausage;
16
17import android.content.Intent;
18import android.content.pm.PackageManager;
19import android.os.Bundle;
20import android.provider.Settings;
21import android.util.Log;
22
23import com.android.settings.R;
24import com.android.settings.SettingsActivity;
25import com.android.settingslib.AppItem;
26
27/**
28 * Standalone activity used to launch {@link AppDataUsage} from a
29 * {@link Settings#ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS} intent.
30 */
31public class AppDataUsageActivity extends SettingsActivity {
32
33    private static final boolean DEBUG = false;
34    private static final String TAG = "AppDataUsageActivity";
35
36    @Override
37    protected void onCreate(Bundle savedInstanceState) {
38        final Intent intent = getIntent();
39        final String packageName = intent.getData().getSchemeSpecificPart();
40        final PackageManager pm = getPackageManager();
41        final int uid;
42        try {
43            uid = pm.getPackageUid(packageName, 0);
44        } catch (PackageManager.NameNotFoundException e) {
45            Log.w(TAG, "invalid package: " + packageName);
46            try {
47                // Activity lifecycle still requires calling onCreate()
48                super.onCreate(savedInstanceState);
49            } catch (Exception e2) {
50                // Ignore - most likely caused by SettingsActivity because of invalid fragment
51                if (DEBUG) Log.d(TAG, "onCreate() exception", e);
52            } finally {
53                finish();
54            }
55            return;
56        }
57        if (DEBUG) Log.d(TAG, "Package: " + packageName + " UID: " + uid);
58
59        final Bundle args = new Bundle();
60        final AppItem appItem = new AppItem(uid);
61        appItem.addUid(uid);
62        args.putParcelable(AppDataUsage.ARG_APP_ITEM, appItem);
63        intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
64        intent.putExtra(EXTRA_SHOW_FRAGMENT, AppDataUsage.class.getName());
65        intent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, R.string.app_data_usage);
66
67        super.onCreate(savedInstanceState);
68    }
69
70    @Override
71    protected boolean isValidFragment(String fragmentName) {
72        return super.isValidFragment(fragmentName)
73                || AppDataUsage.class.getName().equals(fragmentName);
74    }
75}
76