1 /* 2 * Copyright (C) 2021 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.qc; 18 19 import android.net.Uri; 20 import android.os.Bundle; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.Button; 25 import android.widget.EditText; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.Nullable; 29 import androidx.fragment.app.Fragment; 30 31 import com.android.car.qc.controller.RemoteQCController; 32 import com.android.car.qc.view.QCView; 33 34 import com.google.android.car.kitchensink.R; 35 36 public final class QCViewerFragment extends Fragment { 37 private static final String KEY_CURRENT_URI_STRING = "CURRENT_URI_STRING"; 38 39 private RemoteQCController mController; 40 private String mUriString; 41 private EditText mInput; 42 private Button mButton; 43 private QCView mQCView; 44 45 @Override onCreate(@ullable Bundle savedInstanceState)46 public void onCreate(@Nullable Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 if (savedInstanceState != null) { 49 mUriString = savedInstanceState.getString(KEY_CURRENT_URI_STRING); 50 } 51 } 52 53 @Override onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)54 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 55 @Nullable Bundle savedInstanceState) { 56 View v = inflater.inflate(R.layout.qc_viewer, container, false); 57 mInput = v.findViewById(R.id.qc_uri_input); 58 mButton = v.findViewById(R.id.submit_uri_btn); 59 mQCView = v.findViewById(R.id.qc_view); 60 61 mButton.setOnClickListener(v1 -> { 62 mUriString = mInput.getText().toString(); 63 Uri uri = Uri.parse(mUriString); 64 updateQCView(uri); 65 }); 66 67 if (mUriString != null) { 68 Uri uri = Uri.parse(mUriString); 69 updateQCView(uri); 70 } 71 72 return v; 73 } 74 75 @Override onSaveInstanceState(@onNull Bundle outState)76 public void onSaveInstanceState(@NonNull Bundle outState) { 77 super.onSaveInstanceState(outState); 78 outState.putString(KEY_CURRENT_URI_STRING, mUriString); 79 } 80 81 @Override onStart()82 public void onStart() { 83 super.onStart(); 84 if (mController != null) { 85 mController.listen(true); 86 } 87 } 88 89 @Override onStop()90 public void onStop() { 91 super.onStop(); 92 if (mController != null) { 93 mController.listen(false); 94 } 95 } 96 97 @Override onDestroyView()98 public void onDestroyView() { 99 super.onDestroyView(); 100 mUriString = null; 101 if (mController != null) { 102 mController.destroy(); 103 mController = null; 104 } 105 } 106 updateQCView(Uri uri)107 private void updateQCView(Uri uri) { 108 if (uri == null) { 109 return; 110 } 111 if (mController != null) { 112 // destroy old controller 113 mController.destroy(); 114 } 115 116 mController = new RemoteQCController(getContext(), uri); 117 mController.addObserver(mQCView); 118 mController.listen(true); 119 } 120 } 121