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 package com.google.android.car.kitchensink.mainline;
17 
18 import android.car.Car;
19 import android.car.CarVersion;
20 import android.car.PlatformVersion;
21 import android.car.content.pm.CarPackageManager;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageManager.NameNotFoundException;
24 import android.os.Build.VERSION;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.TextView;
31 
32 import androidx.fragment.app.Fragment;
33 
34 import com.google.android.car.kitchensink.KitchenSinkActivity;
35 import com.google.android.car.kitchensink.R;
36 
37 import java.io.FileDescriptor;
38 import java.io.PrintWriter;
39 
40 /**
41  * Displays info about Car Mainline APIs.
42  */
43 public class CarMainlineFragment extends Fragment {
44 
45     private static final String TAG = CarMainlineFragment.class.getSimpleName();
46 
47     // TODO(b/228506662): also add a ListView with existing apps
48 
49     // Reported by SDK
50     private TextView mAppTargetSdk;
51     private TextView mAppCompilationSdk;
52     private TextView mAndroidSdkCodename;
53     private TextView mAndroidSdkVersion;
54 
55     // Reported by Car
56     private TextView mAppCarTargetMajorSdk;
57     private TextView mAppCarTargetMinorSdk;
58     private TextView mPlatformVersion;
59     private TextView mCarVersion;
60     private TextView mCarMajorVersion;
61     private TextView mCarMinorVersion;
62 
63     private CarPackageManager mCarPm;
64 
65     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)66     public View onCreateView(LayoutInflater inflater, ViewGroup container,
67             Bundle savedInstanceState) {
68         View view = inflater.inflate(R.layout.mainline, container, false);
69 
70         mAppTargetSdk = view.findViewById(R.id.app_target_sdk);
71         mAppCompilationSdk = view.findViewById(R.id.app_compilation_sdk);
72         mAppCarTargetMajorSdk = view.findViewById(R.id.app_car_target_major_sdk);
73         mAppCarTargetMinorSdk = view.findViewById(R.id.app_car_target_minor_sdk);
74         mAndroidSdkCodename = view.findViewById(R.id.android_sdk_codename);
75         mAndroidSdkVersion = view.findViewById(R.id.android_sdk_version);
76         mPlatformVersion = view.findViewById(R.id.platform_version);
77         mCarVersion = view.findViewById(R.id.car_version);
78         mCarMajorVersion = view.findViewById(R.id.car_major_version);
79         mCarMinorVersion = view.findViewById(R.id.car_minor_version);
80 
81         return view;
82     }
83 
84     @Override
onStart()85     public void onStart() {
86         super.onStart();
87 
88         ApplicationInfo appInfo = getContext().getApplicationContext().getApplicationInfo();
89         CarVersion carApiVersion = Car.getCarVersion();
90         PlatformVersion platformApiVersion = Car.getPlatformVersion();
91 
92         Car car = ((KitchenSinkActivity) getHost()).getCar();
93         mCarPm = (CarPackageManager) car.getCarManager(Car.PACKAGE_SERVICE);
94 
95         mAppTargetSdk.setText(String.valueOf(appInfo.targetSdkVersion));
96         mAppCompilationSdk.setText(String.valueOf(appInfo.compileSdkVersion));
97         mAndroidSdkCodename.setText(String.valueOf(VERSION.CODENAME));
98         mAndroidSdkVersion.setText(String.valueOf(VERSION.SDK_INT));
99         mCarMajorVersion.setText(String.valueOf(carApiVersion.getMajorVersion()));
100         mCarMinorVersion.setText(String.valueOf(carApiVersion.getMinorVersion()));
101 
102         boolean isCarApiTQpr = carApiVersion.isAtLeast(CarVersion.VERSION_CODES.TIRAMISU_1);
103         Log.v(TAG, "onStart(): isCarApiTQpr=" + isCarApiTQpr);
104         if (isCarApiTQpr) {
105             mPlatformVersion.setText(platformApiVersion.toString());
106             mCarVersion.setText(carApiVersion.toString());
107             setTargetCarApiVersion();
108         } else {
109             // TODO(b/228506662): install on device running T to make sure it works
110             String unsupported = String.format("N/A on %s", carApiVersion);
111             mPlatformVersion.setText(unsupported);
112             mCarVersion.setText(unsupported);
113             mAppCarTargetMajorSdk.setText(unsupported);
114             mAppCarTargetMinorSdk.setText(unsupported);
115         }
116     }
117 
setTargetCarApiVersion()118     private void setTargetCarApiVersion() {
119         String ksPkg = getContext().getPackageName();
120         CarVersion apiVersion;
121         try {
122             apiVersion = mCarPm.getTargetCarVersion(ksPkg);
123         } catch (NameNotFoundException e) {
124             Log.w(TAG, "Could not get target car version for " + ksPkg, e);
125             String text = "N/A";
126             mAppCarTargetMajorSdk.setText(text);
127             mAppCarTargetMinorSdk.setText(text);
128             return;
129         }
130         mAppCarTargetMajorSdk.setText(String.valueOf(apiVersion.getMajorVersion()));
131         mAppCarTargetMinorSdk.setText(String.valueOf(apiVersion.getMinorVersion()));
132     }
133 
134     @Override
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)135     public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
136         writer.printf("%smAppTargetSdk: %s\n", prefix, mAppTargetSdk.getText());
137         writer.printf("%smAppCompilationSdk: %s\n", prefix, mAppCompilationSdk.getText());
138         writer.printf("%smAppCarTargetMajorSdk: %s\n", prefix, mAppCarTargetMajorSdk.getText());
139         writer.printf("%smAppCarTargetMinorSdk: %s\n", prefix, mAppCarTargetMinorSdk.getText());
140         writer.printf("%smAndroidSdkCodename: %s\n", prefix, mAndroidSdkCodename.getText());
141         writer.printf("%smAndroidSdkVersion: %s\n", prefix, mAndroidSdkVersion.getText());
142         writer.printf("%smCarMajorVersion: %s\n", prefix, mCarMajorVersion.getText());
143         writer.printf("%smCarMinorVersion: %s\n", prefix, mCarMinorVersion.getText());
144     }
145 }
146