• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.google.android.tv.btservices;
18 
19 import android.app.Fragment;
20 import android.app.FragmentManager;
21 import android.app.FragmentTransaction;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.media.AudioManager;
25 import android.text.TextUtils;
26 import android.transition.Scene;
27 import android.transition.Slide;
28 import android.transition.Transition;
29 import android.transition.TransitionManager;
30 import android.view.Gravity;
31 import android.view.KeyEvent;
32 import android.view.ViewGroup;
33 import android.view.Window;
34 import java.util.HashMap;
35 import java.util.Map;
36 
37 public class SettingsUtils {
38 
39     private static final String ACTION_CONNECT_INPUT_NORMAL =
40             "com.google.android.intent.action.CONNECT_INPUT";
41     private static final String INTENT_EXTRA_NO_INPUT_MODE = "no_input_mode";
42 
sendPairingIntent(Context context, KeyEvent event)43     public static void sendPairingIntent(Context context, KeyEvent event) {
44         // To be uncommented with enabling new pairing flow
45         // Intent intent = new Intent(context, BluetoothScannerActivity.class);
46         Intent intent = getPairingIntent();
47 
48         if (event != null) {
49             intent.putExtra(INTENT_EXTRA_NO_INPUT_MODE, true)
50                     .putExtra(Intent.EXTRA_KEY_EVENT, event);
51         }
52         context.startActivity(intent);
53     }
54 
getPairingIntent()55     public static Intent getPairingIntent() {
56         return new Intent(ACTION_CONNECT_INPUT_NORMAL).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
57     }
58 
getAudioManagerParameter(Context context, String param)59     public static Map<String, String> getAudioManagerParameter(Context context, String param) {
60         AudioManager am = context.getSystemService(AudioManager.class);
61         String str = am.getParameters(param);
62         Map<String, String> ret = new HashMap<>();
63         if (TextUtils.isEmpty(param)) {
64             return ret;
65         }
66 
67         for (String s: str.split(";")) {
68             String[] keyVal = s.split("=");
69             ret.put(keyVal[0], keyVal[1]);
70         }
71         return ret;
72     }
73 
74     public static class SettingsPanelAnimation {
75 
76         private final FragmentManager mFragmentManager;
77         private final String mTag;
78         private final FragmentFactory mFactory;
79         private final ViewGroup mViewRoot;
80         private final Window mWindow;
81 
82         public interface FragmentFactory {
create()83             Fragment create();
84         }
85 
SettingsPanelAnimation(FragmentManager fm, String tag, ViewGroup root, FragmentFactory factory, Window window)86         public SettingsPanelAnimation(FragmentManager fm, String tag, ViewGroup root,
87                 FragmentFactory factory, Window window) {
88             mFragmentManager = fm;
89             mTag = tag;
90             mFactory = factory;
91             mViewRoot = root;
92             mWindow = window;
93         }
94 
transitionIn()95         public void transitionIn() {
96             Fragment fragment = mFragmentManager.findFragmentByTag(mTag);
97             final boolean replace = (fragment != null);
98             fragment = mFactory.create();
99             FragmentTransaction transact = mFragmentManager.beginTransaction();
100             if (!replace) {
101                 transact.add(android.R.id.content, fragment, mTag);
102             } else {
103                 transact.replace(android.R.id.content, fragment, mTag);
104             }
105             transact.commit();
106             final Scene scene = new Scene(mViewRoot);
107             final Slide slide = new Slide(Gravity.END);
108             TransitionManager.go(scene, slide);
109         }
110 
transitionOut(Runnable afterTransition)111         public void transitionOut(Runnable afterTransition) {
112             final Fragment fragment = mFragmentManager.findFragmentByTag(mTag);
113             if (fragment != null && fragment.isResumed()) {
114                 final Scene scene = new Scene(mViewRoot);
115                 scene.setEnterAction(() -> {
116                     mFragmentManager.beginTransaction()
117                             .remove(fragment)
118                             .commitNow();
119                 });
120                 final Slide slide = new Slide(Gravity.END);
121 
122                 slide.addListener(new Transition.TransitionListener() {
123                     @Override
124                     public void onTransitionStart(Transition transition) {
125                         mWindow.setDimAmount(0);
126                     }
127 
128                     @Override
129                     public void onTransitionEnd(Transition transition) {
130                         transition.removeListener(this);
131                         if (afterTransition != null) {
132                             afterTransition.run();
133                         }
134                     }
135 
136                     @Override
137                     public void onTransitionCancel(Transition transition) {
138                     }
139 
140                     @Override
141                     public void onTransitionPause(Transition transition) {
142                     }
143 
144                     @Override
145                     public void onTransitionResume(Transition transition) {
146                     }
147                 });
148                 TransitionManager.go(scene, slide);
149             } else {
150                 if (afterTransition != null) {
151                     afterTransition.run();
152                 }
153             }
154         }
155     }
156 }
157