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.os.Bundle;
21import android.support.annotation.NonNull;
22import android.support.v17.leanback.app.GuidedStepFragment;
23import android.support.v17.leanback.widget.GuidedAction;
24import android.support.v7.preference.Preference;
25
26import com.android.settingslib.applications.ApplicationsState;
27
28import java.util.List;
29
30public abstract class AppActionPreference extends Preference {
31    protected ApplicationsState.AppEntry mEntry;
32
33    public AppActionPreference(Context context, ApplicationsState.AppEntry entry) {
34        super(context);
35        mEntry = entry;
36    }
37
38    /**
39     * Set entry and refresh pref.
40     * @param entry entry
41     */
42    public void setEntry(@NonNull ApplicationsState.AppEntry entry) {
43        mEntry = entry;
44        refresh();
45    }
46
47    /**
48     * Refresh pref from AppEntry
49     */
50    public abstract void refresh();
51
52    public static abstract class ConfirmationFragment extends GuidedStepFragment {
53        private static final int ID_OK = 0;
54        private static final int ID_CANCEL = 1;
55
56        @Override
57        public void onCreateActions(@NonNull List<GuidedAction> actions,
58                Bundle savedInstanceState) {
59            actions.add(new GuidedAction.Builder()
60                    .title(getString(android.R.string.ok))
61                    .id(ID_OK)
62                    .build());
63            actions.add(new GuidedAction.Builder()
64                    .title(getString(android.R.string.cancel))
65                    .id(ID_CANCEL)
66                    .build());
67        }
68
69        @Override
70        public void onGuidedActionClicked(GuidedAction action) {
71            switch ((int) action.getId()) {
72                case ID_OK:
73                    onOk();
74                    break;
75                case ID_CANCEL:
76                    break;
77            }
78            getFragmentManager().popBackStack();
79        }
80
81        public abstract void onOk();
82    }
83
84}
85