1 /* 2 * Copyright (C) 2018 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.android.car.carlauncher; 18 19 import android.content.Intent; 20 import android.os.Bundle; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.ImageView; 25 import android.widget.TextView; 26 27 import androidx.fragment.app.Fragment; 28 import androidx.lifecycle.ViewModelProviders; 29 30 /** {@link Fragment} which displays relevant information that changes over time. */ 31 public class ContextualFragment extends Fragment { 32 33 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)34 public View onCreateView(LayoutInflater inflater, ViewGroup container, 35 Bundle savedInstanceState) { 36 View rootView = inflater.inflate(R.layout.contextual_fragment, container, false); 37 ImageView iconView = rootView.findViewById(R.id.icon); 38 TextView topLineView = rootView.findViewById(R.id.top_line); 39 TextView bottomLineView = rootView.findViewById(R.id.bottom_line); 40 View dateDividerView = rootView.findViewById(R.id.date_divider); 41 View dateView = rootView.findViewById(R.id.date); 42 View bottomLineContainerView = rootView.findViewById(R.id.bottom_line_container); 43 44 ContextualViewModel viewModel = ViewModelProviders.of(this).get(ContextualViewModel.class); 45 46 viewModel.getContextualInfo().observe(this, info -> { 47 if (info == null) { 48 return; 49 } 50 51 iconView.setImageDrawable(info.getIcon()); 52 topLineView.setText(info.getTopLine()); 53 54 boolean showBottomLineMessage = (info.getBottomLine() != null); 55 56 bottomLineView.setVisibility(showBottomLineMessage ? View.VISIBLE : View.GONE); 57 bottomLineView.setText(info.getBottomLine()); 58 59 dateView.setVisibility(info.getShowClock() ? View.VISIBLE : View.GONE); 60 61 // If both the bottom-line message and the clock are shown, show the divider. 62 dateDividerView.setVisibility( 63 (showBottomLineMessage && info.getShowClock()) ? View.VISIBLE : View.GONE); 64 // Hide the bottom-line container if neither the bottom-line message nor the clock 65 // is being shown. This will center the top-line message in the card. 66 bottomLineContainerView.setVisibility( 67 (showBottomLineMessage || info.getShowClock()) ? View.VISIBLE : View.GONE); 68 69 Intent onClickActivity = info.getOnClickActivity(); 70 View.OnClickListener listener = 71 onClickActivity != null 72 ? v -> startActivity(info.getOnClickActivity()) 73 : null; 74 rootView.setOnClickListener(listener); 75 rootView.setClickable(listener != null); 76 }); 77 78 return rootView; 79 } 80 } 81