1 /*
2  * Copyright (C) 2015 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.cts.verifier.car;
18 
19 import android.app.UiModeManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ActivityInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.os.Bundle;
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.widget.Button;
30 
31 import com.android.cts.verifier.PassFailButtons;
32 import com.android.cts.verifier.R;
33 import com.android.cts.verifier.TestResult;
34 
35 /**
36  * Tests that CAR_DOCK mode opens the app associated with car dock when going into
37  * car mode.
38  */
39 public class CarDockTestActivity extends PassFailButtons.Activity {
40 
41     private static final String CAR_DOCK1 =
42             "com.android.cts.verifier.car.CarDockActivity1";
43     private static final String CAR_DOCK2 =
44             "com.android.cts.verifier.car.CarDockActivity2";
45 
46     private UiModeManager mManager;
47 
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         View view = getLayoutInflater().inflate(R.layout.car_dock_test_main, null);
52         setContentView(view);
53         setInfoResources(R.string.car_dock_test, R.string.car_dock_test_desc, -1);
54         setPassFailButtonClickListeners();
55         getPassButton().setEnabled(false);
56         mManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
57         CarDockActivity.sOnHomePressedRunnable = new Runnable() {
58             @Override
59             public void run() {
60                 TestResult.setPassedResult(CarDockTestActivity.this, getTestId(),
61                         getTestDetails(), getReportLog());
62                 mManager.disableCarMode(0);
63                 finish();
64             }
65         };
66         Button button = (Button) view.findViewById(R.id.car_mode);
67         button.setOnClickListener(new OnClickListener() {
68             @Override
69             public void onClick(View view) {
70                 mManager.enableCarMode(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME);
71             }
72         });
73         Intent i = new Intent(Intent.ACTION_MAIN, null);
74         i.addCategory(Intent.CATEGORY_CAR_DOCK);
75         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
76                 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
77         ActivityInfo ai = null;
78         ResolveInfo info = getPackageManager().resolveActivity(i,
79                 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA);
80         if (info != null) {
81             ai = info.activityInfo;
82             // Check if we are the default CAR_DOCK handler
83             if (!ai.packageName.equals(getPackageName())) {
84                 // Switch components to fake new CAR_DOCK install to force bringing up the
85                 // disambiguation dialog.
86                 PackageManager pm = getApplicationContext().getPackageManager();
87                 ComponentName component1 = new ComponentName(getPackageName(), CAR_DOCK1);
88                 ComponentName component2 = new ComponentName(getPackageName(), CAR_DOCK2);
89 
90                 if (pm.getComponentEnabledSetting(component1) ==
91                         PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
92                     swapCarDockHandler(component2, component1);
93                 } else {
94                     swapCarDockHandler(component1, component2);
95                 }
96             }
97         }
98     }
99 
swapCarDockHandler( ComponentName toBeDisabledComponent, ComponentName toBeEnabledComponent)100     private void swapCarDockHandler(
101             ComponentName toBeDisabledComponent, ComponentName toBeEnabledComponent) {
102         PackageManager pm = getApplicationContext().getPackageManager();
103 
104         pm.setComponentEnabledSetting(toBeDisabledComponent,
105                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
106                 PackageManager.DONT_KILL_APP);
107 
108         pm.setComponentEnabledSetting(toBeEnabledComponent,
109                 PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
110                 PackageManager.DONT_KILL_APP);
111     }
112 }
113