1/*
2 * Copyright (C) 2015 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.tv.settings.device.apps;
18
19import android.content.Context;
20import android.content.pm.ApplicationInfo;
21import android.content.pm.PackageManager;
22import android.os.storage.StorageManager;
23import android.os.storage.VolumeInfo;
24import android.text.TextUtils;
25
26import com.android.settingslib.applications.ApplicationsState;
27import com.android.tv.settings.R;
28
29import java.util.List;
30
31public class AppStoragePreference extends AppActionPreference {
32    private final PackageManager mPackageManager;
33    private final StorageManager mStorageManager;
34
35    public AppStoragePreference(Context context, ApplicationsState.AppEntry entry) {
36        super(context, entry);
37        mPackageManager = context.getPackageManager();
38        mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
39        refresh();
40    }
41
42    public void refresh() {
43        final ApplicationInfo applicationInfo = mEntry.info;
44        final VolumeInfo volumeInfo = mPackageManager.getPackageCurrentVolume(applicationInfo);
45        final List<VolumeInfo> candidates =
46                mPackageManager.getPackageCandidateVolumes(applicationInfo);
47        if (candidates.size() > 1 ||
48                (candidates.size() == 1 && !candidates.contains(volumeInfo))) {
49            setIntent(MoveAppActivity
50                    .getLaunchIntent(getContext(), mEntry.info.packageName, getAppName()));
51        }
52
53        setTitle(R.string.device_apps_app_management_storage_used);
54
55        final String volumeDesc = mStorageManager.getBestVolumeDescription(volumeInfo);
56        final String size = mEntry.sizeStr;
57        if (TextUtils.isEmpty(size)) {
58            setSummary(R.string.storage_calculating_size);
59        } else {
60            setSummary(getContext().getString(R.string.device_apps_app_management_storage_used_desc,
61                    mEntry.sizeStr, volumeDesc));
62        }
63
64    }
65
66    private String getAppName() {
67        mEntry.ensureLabel(getContext());
68        return mEntry.label;
69    }
70}
71