1 /*
2  * Copyright (C) 2019 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;
18 
19 import android.car.Car;
20 import android.content.ComponentName;
21 import android.content.ServiceConnection;
22 import android.os.Bundle;
23 import android.os.IBinder;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.TextView;
28 
29 import androidx.fragment.app.Fragment;
30 
31 public class CarApiTestFragment extends Fragment {
32 
33     private static final String TAG = CarApiTestFragment.class.getSimpleName();
34 
35     private Car mCarForCreateAndConnect;
36     private Car mCarForCreateCar;
37     private Car mCarForStatusChange;
38 
39     private TextView mTextForCreateAndConnect;
40     private TextView mTextForCreateCar;
41     private TextView mTextForCreateCarWithStatusChangeListener;
42 
43     private int mConnectCountForStatusChange = 0;
44 
45     private final ServiceConnection mServiceConnectionForCreateAndConnect =
46             new ServiceConnection() {
47 
48                 private int mConnectCount = 0;
49 
50                 @Override
51                 public void onServiceConnected(ComponentName name, IBinder service) {
52                     mConnectCount++;
53                     mTextForCreateAndConnect.setText("bound service connected, isConnected:"
54                             + mCarForCreateAndConnect.isConnected()
55                             + " connect count:" + mConnectCount);
56                 }
57 
58                 @Override
59                 public void onServiceDisconnected(ComponentName name) {
60                     mTextForCreateAndConnect.setText("bound service disconnected, isConnected:"
61                             + mCarForCreateAndConnect.isConnected());
62                 }
63             };
64 
65     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)66     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
67         View view = inflater.inflate(R.layout.carapi, container, false);
68         mTextForCreateAndConnect = view.findViewById(R.id.carapi_createandconnect);
69         mTextForCreateCar = view.findViewById(R.id.carapi_createcar);
70         mTextForCreateCarWithStatusChangeListener = view.findViewById(
71                 R.id.carapi_createcar_with_status_change);
72         view.findViewById(R.id.button_carapi_createandconnect).setOnClickListener(
73                 (View v) -> {
74                     disconnectCar(mCarForCreateAndConnect);
75                     mCarForCreateAndConnect = Car.createCar(getContext(),
76                             mServiceConnectionForCreateAndConnect);
77                     mCarForCreateAndConnect.connect();
78                 });
79         view.findViewById(R.id.button_carapi_createcar).setOnClickListener(
80                 (View v) -> {
81                     disconnectCar(mCarForCreateCar);
82                     mCarForCreateCar = Car.createCar(getContext());
83                     mTextForCreateCar.setText("isConnected:" + mCarForCreateCar.isConnected());
84                 });
85         view.findViewById(R.id.button_carapi_createcar_with_status_change).setOnClickListener(
86                 (View v) -> {
87                     disconnectCar(mCarForStatusChange);
88                     mCarForStatusChange = Car.createCar(getContext(), null,
89                             Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER,
90                             (Car car, boolean ready) -> {
91                                 if (ready) {
92                                     mConnectCountForStatusChange++;
93                                     mTextForCreateCarWithStatusChangeListener.setText(
94                                             "service ready, isConnected:"
95                                                     + car.isConnected()
96                                                     + " connect count:"
97                                                     + mConnectCountForStatusChange);
98                                 } else {
99                                     mTextForCreateCarWithStatusChangeListener.setText(
100                                             "bound service crashed, isConnected:"
101                                                     + car.isConnected());
102                                 }
103                             });
104                 });
105         return view;
106     }
107 
108     @Override
onDestroyView()109     public void onDestroyView() {
110         disconnectCar(mCarForCreateAndConnect);
111         mCarForCreateAndConnect = null;
112         disconnectCar(mCarForCreateCar);
113         mCarForCreateCar = null;
114         disconnectCar(mCarForStatusChange);
115         mCarForStatusChange = null;
116         super.onDestroyView();
117     }
118 
disconnectCar(Car car)119     private void disconnectCar(Car car) {
120         if (car != null && car.isConnected()) {
121             car.disconnect();
122         }
123     }
124 }
125