1 /*
2  * Copyright (C) 2018 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.activityview;
18 
19 import android.car.app.CarActivityView;
20 import android.content.ComponentName;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.SystemClock;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 
28 import androidx.annotation.Nullable;
29 import androidx.fragment.app.Fragment;
30 
31 import com.google.android.car.kitchensink.R;
32 
33 import java.util.Random;
34 
35 /**
36  * This fragment exercises the capabilities of virtual display.
37  */
38 public class ActivityViewTestFragment extends Fragment {
39     private CarActivityView mActivityView;
40     private ViewGroup mActivityViewParent;
41 
42     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)43     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
44             @Nullable Bundle savedInstanceState) {
45         View root = inflater.inflate(R.layout.activity_view_test_fragment, container, false);
46 
47         root.findViewById(R.id.av_launch_activity)
48                 .setOnClickListener(this::onLaunchActivityClicked);
49         root.findViewById(R.id.av_resize)
50                 .setOnClickListener(this::onResizeVirtualDisplayClicked);
51 
52         mActivityView = root.findViewById(R.id.av_activityview);
53         mActivityViewParent = ((ViewGroup) mActivityView.getParent());
54 
55         return root;
56     }
57 
onResizeVirtualDisplayClicked(View v)58     private void onResizeVirtualDisplayClicked(View v) {
59         mActivityViewParent.setPadding(20, 20, 20,
60                 new Random(SystemClock.elapsedRealtimeNanos()).nextInt(200));
61     }
62 
onLaunchActivityClicked(View v)63     private void onLaunchActivityClicked(View v) {
64         Intent intent = new Intent();
65         intent.setComponent(ComponentName.createRelative("com.android.car.settings", ".Settings"));
66         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
67         mActivityView.startActivity(intent);
68     }
69 
70     @Override
onDestroy()71     public void onDestroy() {
72         super.onDestroy();
73         mActivityView.release();
74     }
75 }
76