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;
21import android.util.Log;
22
23import java.util.HashMap;
24import java.util.List;
25
26public class GuidedStepAttributesTestFragment extends GuidedStepFragment {
27
28    private static String TAG = "GuidedStepAttributesTestFragment";
29    static class Callback {
30        public void onActionClicked(GuidedStepFragment fragment, long id) {
31        }
32    }
33
34    static HashMap<Long, Callback> sCallbacks = new HashMap();
35    public static GuidanceStylist.Guidance GUIDANCE = null;
36    public static List<GuidedAction> ACTION_LIST = null;
37    public static long LAST_CLICKED_ACTION_ID = -1;
38    public static long LAST_SELECTED_ACTION_ID = -1;
39
40    @Override
41    public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
42        if (GUIDANCE == null ) {
43            return new GuidanceStylist.Guidance("", "", "", null);
44        }
45        return GUIDANCE;
46    }
47
48    @Override
49    public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
50        if (ACTION_LIST == null)
51            return;
52        actions.addAll(ACTION_LIST);
53    }
54
55    @Override
56    public void onGuidedActionClicked(GuidedAction action) {
57        super.onGuidedActionFocused(action);
58        Callback callback = sCallbacks.get(action.getId());
59        if (callback != null) {
60            callback.onActionClicked(this, action.getId());
61        } else {
62            LAST_CLICKED_ACTION_ID = action.getId();
63        }
64    }
65
66    @Override
67    public void onGuidedActionFocused(GuidedAction action) {
68        super.onGuidedActionFocused(action);
69        LAST_SELECTED_ACTION_ID = action.getId();
70    }
71
72    @Override
73    public boolean onSubGuidedActionClicked(GuidedAction action) {
74        super.onSubGuidedActionClicked(action);
75        LAST_CLICKED_ACTION_ID = action.getId();
76        return true;
77    }
78
79    @Override
80    public long onGuidedActionEditedAndProceed(GuidedAction action) {
81
82        Callback callback = sCallbacks.get(action.getId());
83        if (callback != null) {
84            callback.onActionClicked(this, action.getId());
85        } else {
86            super.onGuidedActionEditedAndProceed(action);
87        }
88        return GuidedAction.ACTION_ID_CURRENT;
89    }
90
91    public static void setActionClickCallback(long id, Callback callback) {
92        sCallbacks.put(id, callback);
93    }
94
95    public static void clear() {
96        LAST_CLICKED_ACTION_ID = -1;
97        LAST_SELECTED_ACTION_ID = -1;
98        sCallbacks.clear();
99    }
100}
101