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