1 /*
2  * Copyright (C) 2017 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 
17 package com.android.tv.dvr.ui;
18 
19 import android.content.Context;
20 import androidx.leanback.app.GuidedStepFragment;
21 import androidx.leanback.widget.GuidedAction;
22 import com.android.tv.TvSingletons;
23 import com.android.tv.analytics.Tracker;
24 
25 /** A {@link GuidedStepFragment} with {@link Tracker} for analytics. */
26 public abstract class TrackedGuidedStepFragment extends GuidedStepFragment {
27     private Tracker mTracker;
28 
29     @Override
onAttach(Context context)30     public void onAttach(Context context) {
31         super.onAttach(context);
32         mTracker = TvSingletons.getSingletons(context).getAnalytics().getDefaultTracker();
33     }
34 
35     @Override
onDetach()36     public void onDetach() {
37         mTracker = null;
38         super.onDetach();
39     }
40 
41     @Override
onGuidedActionClicked(GuidedAction action)42     public final void onGuidedActionClicked(GuidedAction action) {
43         super.onGuidedActionClicked(action);
44         if (mTracker != null) {
45             mTracker.sendMenuClicked(
46                     getTrackerPrefix() + "-action-" + getTrackerLabelForGuidedAction(action));
47         }
48         onTrackedGuidedActionClicked(action);
49     }
50 
getTrackerLabelForGuidedAction(GuidedAction action)51     public String getTrackerLabelForGuidedAction(GuidedAction action) {
52         long actionId = action.getId();
53         if (actionId == GuidedAction.ACTION_ID_CANCEL) {
54             return "cancel";
55         } else if (actionId == GuidedAction.ACTION_ID_NEXT) {
56             return "next";
57         } else if (actionId == GuidedAction.ACTION_ID_CURRENT) {
58             return "current";
59         } else if (actionId == GuidedAction.ACTION_ID_OK) {
60             return "ok";
61         } else if (actionId == GuidedAction.ACTION_ID_CANCEL) {
62             return "cancel";
63         } else if (actionId == GuidedAction.ACTION_ID_FINISH) {
64             return "finish";
65         } else if (actionId == GuidedAction.ACTION_ID_CONTINUE) {
66             return "continue";
67         } else if (actionId == GuidedAction.ACTION_ID_YES) {
68             return "yes";
69         } else if (actionId == GuidedAction.ACTION_ID_NO) {
70             return "no";
71         } else {
72             return "unknown-" + actionId;
73         }
74     }
75 
76     /** Delegated from {@link #onGuidedActionClicked(GuidedAction)} */
onTrackedGuidedActionClicked(GuidedAction action)77     public abstract void onTrackedGuidedActionClicked(GuidedAction action);
78 
79     /** The prefix used for analytics tracking, Usually the class name. */
getTrackerPrefix()80     public abstract String getTrackerPrefix();
81 }
82