• 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.android.car.activityresolver;
18 
19 import android.os.Bundle;
20 import android.view.View;
21 import android.view.ViewTreeObserver;
22 import android.widget.ListView;
23 
24 import com.android.internal.R;
25 import com.android.internal.app.ResolverActivity;
26 import com.android.internal.app.ResolverViewPager;
27 
28 /**
29  * An automotive variant of the resolver activity which does not use the safe forwarding mode and
30  * which supports rotary.
31  */
32 public final class CarResolverActivity extends ResolverActivity
33         implements ViewTreeObserver.OnGlobalLayoutListener {
34 
35     private ResolverViewPager mProfilePager;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40 
41         setSafeForwardingMode(false);
42 
43         mProfilePager = findViewById(R.id.profile_pager);
44         mProfilePager.getViewTreeObserver().addOnGlobalLayoutListener(this);
45     }
46 
47     @Override
onDestroy()48     protected void onDestroy() {
49         mProfilePager.getViewTreeObserver().removeOnGlobalLayoutListener(this);
50 
51         super.onDestroy();
52     }
53 
54     @Override
onGlobalLayout()55     public void onGlobalLayout() {
56         ListView listView = findViewById(R.id.resolver_list);
57         if (listView != null) {
58             // Items must be focusable for rotary.
59             listView.setItemsCanFocus(true);
60 
61             // Set click listeners for rotary.
62             for (int i = 0; i < listView.getChildCount(); i++) {
63                 View element = listView.getChildAt(i);
64                 element.setOnClickListener(view -> {
65                     int position = listView.getPositionForView(view);
66                     long id = listView.getItemIdAtPosition(position);
67                     listView.performItemClick(view, position, id);
68                 });
69             }
70         }
71     }
72 }
73