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 except
5 * 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 License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package android.support.v17.leanback.app.wizard;
16
17import android.os.Bundle;
18import android.support.v17.leanback.app.GuidedStepFragment;
19import android.support.v17.leanback.widget.GuidanceStylist;
20import android.support.v17.leanback.widget.GuidedAction;
21
22import java.util.HashMap;
23import java.util.List;
24
25public class GuidedStepAttributesTestFragment extends GuidedStepFragment {
26
27    private static String TAG = "GuidedStepAttributesTestFragment";
28    static class Callback {
29        public void onActionClicked(GuidedStepFragment fragment, long id) {
30        }
31    }
32
33    static HashMap<Long, Callback> sCallbacks = new HashMap();
34    public static GuidanceStylist.Guidance GUIDANCE = null;
35    public static List<GuidedAction> ACTION_LIST = null;
36    public static long LAST_CLICKED_ACTION_ID = -1;
37    public static long LAST_SELECTED_ACTION_ID = -1;
38
39    @Override
40    public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
41        if (GUIDANCE == null ) {
42            return new GuidanceStylist.Guidance("", "", "", null);
43        }
44        return GUIDANCE;
45    }
46
47    @Override
48    public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
49        if (ACTION_LIST == null)
50            return;
51        actions.addAll(ACTION_LIST);
52    }
53
54    @Override
55    public void onGuidedActionClicked(GuidedAction action) {
56        super.onGuidedActionFocused(action);
57        Callback callback = sCallbacks.get(action.getId());
58        if (callback != null) {
59            callback.onActionClicked(this, action.getId());
60        } else {
61            LAST_CLICKED_ACTION_ID = action.getId();
62        }
63    }
64
65    @Override
66    public void onGuidedActionFocused(GuidedAction action) {
67        super.onGuidedActionFocused(action);
68        LAST_SELECTED_ACTION_ID = action.getId();
69    }
70
71    @Override
72    public boolean onSubGuidedActionClicked(GuidedAction action) {
73        super.onSubGuidedActionClicked(action);
74        LAST_CLICKED_ACTION_ID = action.getId();
75        return true;
76    }
77
78    @Override
79    public long onGuidedActionEditedAndProceed(GuidedAction action) {
80
81        Callback callback = sCallbacks.get(action.getId());
82        if (callback != null) {
83            callback.onActionClicked(this, action.getId());
84        } else {
85            super.onGuidedActionEditedAndProceed(action);
86        }
87        return GuidedAction.ACTION_ID_CURRENT;
88    }
89
90    public static void setActionClickCallback(long id, Callback callback) {
91        sCallbacks.put(id, callback);
92    }
93
94    public static void clear() {
95        LAST_CLICKED_ACTION_ID = -1;
96        LAST_SELECTED_ACTION_ID = -1;
97        sCallbacks.clear();
98    }
99}
100