1 /*
2  * Copyright (C) 2022 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.car.kitchensink.hotword;
18 
19 import static com.google.android.car.kitchensink.R.id.services_recycler_view;
20 import static com.google.android.car.kitchensink.R.layout;
21 
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.pm.PackageManager;
25 import android.media.AudioManager;
26 import android.os.Bundle;
27 import android.util.Log;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 
32 import androidx.fragment.app.Fragment;
33 import androidx.recyclerview.widget.LinearLayoutManager;
34 import androidx.recyclerview.widget.RecyclerView;
35 
36 public final class CarMultiConcurrentHotwordTestFragment extends Fragment {
37 
38     private static final String TAG = "HotwordTestFragment";
39 
40     public static final String HOTWORD_DETECTION_SERVICE_PACKAGE =
41             "com.android.car.test.one.hotworddetectionservice";
42 
43     public static final String HOTWORD_DETECTION_SERVICE_ONE =
44             "com.android.car.test.one.hotworddetectionservice.HotwordDetectionServiceOne";
45 
46     private RecyclerView mRecyclerView;
47     private HotwordServiceAdapter mHotwordServiceAdapter;
48 
49     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)50     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
51         Log.i(TAG, "onCreateView");
52         View view = inflater
53                 .inflate(layout.concurrent_hotword, container, /* attachToRoot= */ false);
54 
55         mHotwordServiceAdapter = new HotwordServiceAdapter(createServiceManagers());
56 
57         mRecyclerView = view.findViewById(services_recycler_view);
58         mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
59         mRecyclerView.scrollToPosition(0);
60         mRecyclerView.setAdapter(mHotwordServiceAdapter);
61 
62         return view;
63     }
64 
createServiceManagers()65     private HotwordServiceManager[] createServiceManagers() {
66         Context context = getContext();
67         AudioManager audioManager = context.getSystemService(AudioManager.class);
68         PackageManager packageManager = context.getPackageManager();
69         ComponentName componentNameOne = new ComponentName(HOTWORD_DETECTION_SERVICE_PACKAGE,
70                 HOTWORD_DETECTION_SERVICE_ONE);
71 
72         return new HotwordServiceManager[] {
73                 new HotwordServiceManager(componentNameOne, context, audioManager, packageManager)
74         };
75     }
76 
77     @Override
onDestroyView()78     public void onDestroyView() {
79         super.onDestroyView();
80         Log.i(TAG, "onDestroyView");
81 
82         mHotwordServiceAdapter.release();
83     }
84 }
85