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.storage;
18
19import android.content.pm.PackageInfo;
20import android.content.pm.PackageManager;
21import android.os.Bundle;
22import android.support.annotation.NonNull;
23import android.support.v17.leanback.app.GuidedStepFragment;
24import android.support.v17.leanback.widget.GuidanceStylist;
25import android.support.v17.leanback.widget.GuidedAction;
26
27import com.android.tv.settings.R;
28
29import java.util.List;
30
31public class ConfirmClearCacheFragment extends GuidedStepFragment {
32
33    @NonNull
34    @Override
35    public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
36        return new GuidanceStylist.Guidance(
37                getString(R.string.device_storage_clear_cache_title),
38                getString(R.string.device_storage_clear_cache_message),
39                null,
40                getContext().getDrawable(R.drawable.ic_settings_backup_restore_132dp)
41        );
42    }
43
44    @Override
45    public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
46        actions.add(new GuidedAction.Builder(getContext())
47                .clickAction(GuidedAction.ACTION_ID_OK)
48                .build());
49        actions.add(new GuidedAction.Builder(getContext())
50                .clickAction(GuidedAction.ACTION_ID_CANCEL)
51                .build());
52    }
53
54    @Override
55    public void onGuidedActionClicked(GuidedAction action) {
56        if (action.getId() == GuidedAction.ACTION_ID_OK) {
57            super.onGuidedActionClicked(action);
58            final PackageManager pm = getContext().getPackageManager();
59            final List<PackageInfo> infos = pm.getInstalledPackages(0);
60            for (PackageInfo info : infos) {
61                pm.deleteApplicationCacheFiles(info.packageName, null);
62            }
63            getFragmentManager().popBackStack();
64        } else {
65            getFragmentManager().popBackStack();
66        }
67    }
68}
69