1 /* 2 * Copyright (C) 2016 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.deskclock.controller; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.support.annotation.StringRes; 22 23 import com.android.deskclock.Utils; 24 import com.android.deskclock.events.EventTracker; 25 26 import static com.android.deskclock.Utils.enforceMainLooper; 27 28 /** 29 * Interactions with Android framework components responsible for part of the user experience are 30 * handled via this singleton. 31 */ 32 public final class Controller { 33 34 private static final Controller sController = new Controller(); 35 36 private Context mContext; 37 38 /** The controller that dispatches app events to event trackers. */ 39 private EventController mEventController; 40 41 /** The controller that interacts with voice interaction sessions on M+. */ 42 private VoiceController mVoiceController; 43 44 /** The controller that creates and updates launcher shortcuts on N MR1+ */ 45 private ShortcutController mShortcutController; 46 Controller()47 private Controller() {} 48 getController()49 public static Controller getController() { 50 return sController; 51 } 52 setContext(Context context)53 public void setContext(Context context) { 54 if (mContext != context) { 55 mContext = context.getApplicationContext(); 56 mEventController = new EventController(); 57 mVoiceController = new VoiceController(); 58 if (Utils.isNMR1OrLater()) { 59 mShortcutController = new ShortcutController(mContext); 60 } 61 } 62 } 63 64 // 65 // Event Tracking 66 // 67 68 /** 69 * @param eventTracker to be registered for tracking application events 70 */ addEventTracker(EventTracker eventTracker)71 public void addEventTracker(EventTracker eventTracker) { 72 enforceMainLooper(); 73 mEventController.addEventTracker(eventTracker); 74 } 75 76 /** 77 * @param eventTracker to be unregistered from tracking application events 78 */ removeEventTracker(EventTracker eventTracker)79 public void removeEventTracker(EventTracker eventTracker) { 80 enforceMainLooper(); 81 mEventController.removeEventTracker(eventTracker); 82 } 83 84 /** 85 * Tracks an event. Events have a category, action and label. This method can be used to track 86 * events such as button presses or other user interactions with your application. 87 * 88 * @param category resource id of event category 89 * @param action resource id of event action 90 * @param label resource id of event label 91 */ sendEvent(@tringRes int category, @StringRes int action, @StringRes int label)92 public void sendEvent(@StringRes int category, @StringRes int action, @StringRes int label) { 93 mEventController.sendEvent(category, action, label); 94 } 95 96 // 97 // Voice Interaction 98 // 99 notifyVoiceSuccess(Activity activity, String message)100 public void notifyVoiceSuccess(Activity activity, String message) { 101 mVoiceController.notifyVoiceSuccess(activity, message); 102 } 103 notifyVoiceFailure(Activity activity, String message)104 public void notifyVoiceFailure(Activity activity, String message) { 105 mVoiceController.notifyVoiceFailure(activity, message); 106 } 107 108 // 109 // Shortcuts 110 // 111 updateShortcuts()112 public void updateShortcuts() { 113 enforceMainLooper(); 114 if (mShortcutController != null) { 115 mShortcutController.updateShortcuts(); 116 } 117 } 118 }