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