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.content.Context; 23 import android.os.Bundle; 24 import android.util.Log; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.ViewGroup; 28 import android.widget.Button; 29 import android.widget.TextView; 30 31 import androidx.fragment.app.Fragment; 32 33 import com.google.android.car.kitchensink.KitchenSinkHelper; 34 import com.google.android.car.kitchensink.R; 35 36 import org.jetbrains.annotations.NotNull; 37 38 public class ExperimentalFeatureTestFragment extends Fragment { 39 40 private static final String TAG = "ExperimentalFeature"; 41 42 private static final String[] PING_MSGS = { 43 "Hello, world", 44 "This is 1st experimental feature", 45 }; 46 47 private int mCurrentMsgIndex = 0; 48 private TextView mPingMsgTextView; 49 private Button mPingButton; 50 51 private CarTestDemoExperimentalFeatureManager mDemoManager; 52 private KitchenSinkHelper mKitchenSinkHelper; 53 54 @Override onAttach(@ndroidx.annotation.NonNull @otNull Context context)55 public void onAttach(@androidx.annotation.NonNull @NotNull Context context) { 56 super.onAttach(context); 57 if (!(context instanceof KitchenSinkHelper)) { 58 throw new IllegalStateException( 59 "Attached activity does not implement " 60 + KitchenSinkHelper.class.getSimpleName()); 61 } 62 mKitchenSinkHelper = (KitchenSinkHelper) context; 63 } 64 65 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)66 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) { 67 View view = inflater.inflate(R.layout.experimental_feature_test, container, false); 68 mPingMsgTextView = view.findViewById(R.id.experimental_ping_msg); 69 mPingButton = view.findViewById(R.id.button_experimental_ping); 70 Car car = mKitchenSinkHelper.getCar(); 71 if (car.isFeatureEnabled(ExperimentalCar.TEST_EXPERIMENTAL_FEATURE_SERVICE)) { 72 mDemoManager = (CarTestDemoExperimentalFeatureManager) car.getCarManager( 73 ExperimentalCar.TEST_EXPERIMENTAL_FEATURE_SERVICE); 74 mPingMsgTextView.setText("feature enabled"); 75 } else { 76 Log.w(TAG, "ExperimentalCar.TEST_EXPERIMENTAL_FEATURE_SERVICE not enabled"); 77 mPingButton.setActivated(false); 78 mPingMsgTextView.setText("feature disabled"); 79 } 80 view.findViewById(R.id.button_experimental_ping).setOnClickListener( 81 (View v) -> { 82 if (mDemoManager == null) { 83 return; 84 } 85 String msg = pickMsg(); 86 mPingMsgTextView.setText(mDemoManager.ping(msg)); 87 }); 88 return view; 89 } 90 pickMsg()91 private String pickMsg() { 92 String msg = PING_MSGS[mCurrentMsgIndex]; 93 mCurrentMsgIndex++; 94 if (mCurrentMsgIndex >= PING_MSGS.length) { 95 mCurrentMsgIndex = 0; 96 } 97 return msg; 98 } 99 } 100