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.experimental;
18 
19 import android.car.Car;
20 import android.car.experimental.CarTestDemoExperimentalFeatureManager;
21 import android.car.experimental.ExperimentalCar;
22 import android.os.Bundle;
23 import android.util.Log;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.Button;
28 import android.widget.TextView;
29 
30 import androidx.fragment.app.Fragment;
31 
32 import com.google.android.car.kitchensink.KitchenSinkActivity;
33 import com.google.android.car.kitchensink.R;
34 
35 public class ExperimentalFeatureTestFragment extends Fragment {
36 
37     private static final String TAG = "ExperimentalFeature";
38 
39     private static final String[] PING_MSGS = {
40             "Hello, world",
41             "This is 1st experimental feature",
42     };
43 
44     private int mCurrentMsgIndex = 0;
45     private TextView mPingMsgTextView;
46     private Button mPingButton;
47 
48     private CarTestDemoExperimentalFeatureManager mDemoManager;
49 
50     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)51     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
52         View view = inflater.inflate(R.layout.experimental_feature_test, container, false);
53         mPingMsgTextView = view.findViewById(R.id.experimental_ping_msg);
54         mPingButton = view.findViewById(R.id.button_experimental_ping);
55         Car car = ((KitchenSinkActivity) getHost()).getCar();
56         if (car.isFeatureEnabled(ExperimentalCar.TEST_EXPERIMENTAL_FEATURE_SERVICE)) {
57             mDemoManager = (CarTestDemoExperimentalFeatureManager) car.getCarManager(
58                     ExperimentalCar.TEST_EXPERIMENTAL_FEATURE_SERVICE);
59             mPingMsgTextView.setText("feature enabled");
60         } else {
61             Log.w(TAG, "ExperimentalCar.TEST_EXPERIMENTAL_FEATURE_SERVICE not enabled");
62             mPingButton.setActivated(false);
63             mPingMsgTextView.setText("feature disabled");
64         }
65         view.findViewById(R.id.button_experimental_ping).setOnClickListener(
66                 (View v) -> {
67                     if (mDemoManager == null) {
68                         return;
69                     }
70                     String msg = pickMsg();
71                     mPingMsgTextView.setText(mDemoManager.ping(msg));
72                 });
73         return view;
74     }
75 
pickMsg()76     private String pickMsg() {
77         String msg = PING_MSGS[mCurrentMsgIndex];
78         mCurrentMsgIndex++;
79         if (mCurrentMsgIndex >= PING_MSGS.length) {
80             mCurrentMsgIndex = 0;
81         }
82         return msg;
83     }
84 }
85