1 /* 2 * Copyright (C) 2024 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.launcher3.util; 18 19 import android.content.Context; 20 import android.os.Vibrator; 21 22 import com.android.launcher3.dagger.ApplicationContext; 23 import com.android.launcher3.dagger.LauncherAppSingleton; 24 import com.android.launcher3.dagger.LauncherBaseAppComponent; 25 26 import com.google.android.msdl.data.model.MSDLToken; 27 import com.google.android.msdl.domain.InteractionProperties; 28 import com.google.android.msdl.domain.MSDLPlayer; 29 import com.google.android.msdl.logging.MSDLEvent; 30 31 import java.io.PrintWriter; 32 import java.util.List; 33 34 import javax.inject.Inject; 35 36 /** 37 * Wrapper around {@link com.google.android.msdl.domain.MSDLPlayer} to perform MSDL feedback. 38 */ 39 @LauncherAppSingleton 40 public class MSDLPlayerWrapper { 41 42 public static final DaggerSingletonObject<MSDLPlayerWrapper> INSTANCE = 43 new DaggerSingletonObject<>(LauncherBaseAppComponent::getMSDLPlayerWrapper); 44 45 /** Internal player */ 46 private final MSDLPlayer mMSDLPlayer; 47 48 @Inject MSDLPlayerWrapper(@pplicationContext Context context)49 public MSDLPlayerWrapper(@ApplicationContext Context context) { 50 Vibrator vibrator = context.getSystemService(Vibrator.class); 51 mMSDLPlayer = MSDLPlayer.Companion.createPlayer(vibrator, 52 java.util.concurrent.Executors.newSingleThreadExecutor(), 53 null /* useHapticFeedbackForToken */); 54 } 55 56 /** Perform MSDL feedback for a token with interaction properties */ playToken(MSDLToken token, InteractionProperties properties)57 public void playToken(MSDLToken token, InteractionProperties properties) { 58 mMSDLPlayer.playToken(token, properties); 59 } 60 61 /** Perform MSDL feedback for a token without properties */ playToken(MSDLToken token)62 public void playToken(MSDLToken token) { 63 mMSDLPlayer.playToken(token, null); 64 } 65 getHistory()66 public List<MSDLEvent> getHistory() { 67 return mMSDLPlayer.getHistory(); 68 } 69 70 /** Print the latest history of MSDL tokens played */ dump(String prefix, PrintWriter writer)71 public void dump(String prefix, PrintWriter writer) { 72 writer.println(prefix + mMSDLPlayer.toString()); 73 writer.println(prefix + "MSDLPlayerWrapper history of latest events:"); 74 List<MSDLEvent> events = getHistory(); 75 for (MSDLEvent event: events) { 76 writer.println(prefix + "\t" + event); 77 } 78 } 79 } 80