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.example.android.launchonprivatedisplay;
18 
19 import android.app.Activity;
20 import android.app.ActivityOptions;
21 import android.content.ComponentName;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.widget.AdapterView;
26 import android.widget.ArrayAdapter;
27 import android.widget.Button;
28 import android.widget.Spinner;
29 
30 
31 /**
32  * Activity which would launch an activity on the private display.
33  */
34 public final class MainActivity extends Activity {
35 
36     private static final String NAMESPACE_KEY = "com.android.car.app.private_display";
37     private static final String LAUNCH_ON_PRIVATE_DISPLAY =
38             NAMESPACE_KEY + ".launch_on_private_display";
39     private static final ComponentName LAUNCHED_ACTIVITY_COMPONENT_NAME = new ComponentName(
40             "com.example.android.launchonprivatedisplay",
41             "com.example.android.launchonprivatedisplay.ActivityForPrivateDisplay");
42     private Button mButton;
43     private Spinner mSpinner;
44     private String[] mDisplayUniqueIds;
45     private String[] mDisplayNames;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     public void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         // TODO(b/344585656): Add an edit text to specify the activity name
51         // TODO(b/344585656): Improve spinner text size
52         setContentView(R.layout.main_activity);
53 
54         mButton = findViewById(R.id.button_view);
55         mSpinner = findViewById(R.id.spinner_view);
56 
57         mDisplayUniqueIds = getResources().getStringArray(R.array.distant_display_unique_ids);
58         mDisplayNames = getResources().getStringArray(R.array.distant_display_names);
59 
60         createSpinnerView();
61 
62         mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
63             @Override
64             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
65                 // no-op
66             }
67 
68             @Override
69             public void onNothingSelected(AdapterView<?> parent) {
70                 // no-op
71             }
72         });
73 
74         mButton.setOnClickListener(v -> {
75             int selectedItemPosition = mSpinner.getSelectedItemPosition();
76             String selectedOption = mDisplayUniqueIds[selectedItemPosition];
77 
78             final Intent intent = new Intent();
79             intent.setComponent(LAUNCHED_ACTIVITY_COMPONENT_NAME);
80             Bundle bundle = new Bundle();
81             bundle.putString(LAUNCH_ON_PRIVATE_DISPLAY, selectedOption);
82             ActivityOptions options = ActivityOptions.makeBasic();
83             intent.putExtras(bundle);
84             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
85             startActivity(intent, options.toBundle());
86         });
87     }
88 
createSpinnerView()89     private void createSpinnerView() {
90         ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
91                 android.R.layout.simple_spinner_item, mDisplayNames);
92         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
93         mSpinner.setAdapter(adapter);
94     }
95 }
96