1 /* 2 * Copyright (C) 2020 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 package com.android.car.rotaryplayground; 17 18 import android.os.Bundle; 19 import android.util.Log; 20 import android.view.View; 21 import android.widget.Toast; 22 23 import androidx.fragment.app.Fragment; 24 import androidx.fragment.app.FragmentActivity; 25 26 import java.util.Random; 27 28 /** 29 * The main activity for Rotary Playground 30 * 31 * This activity starts the menu fragment and handles event calls (e.g. android:onClick) from 32 * elements in fragments. 33 */ 34 public class RotaryActivity extends FragmentActivity { 35 36 private static final String TAG = "RotaryActivity"; 37 38 private final Random mRandom = new Random(); 39 40 private Fragment mMenuFragment = null; 41 42 @Override onCreate(Bundle savedInstanceState)43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.rotary_activity); 46 showMenuFragment(); 47 } 48 49 /** Event handler for button clicks. */ onRotaryButtonClick(View v)50 public void onRotaryButtonClick(View v) { 51 final String[] greetings = getResources().getStringArray(R.array.greetings); 52 showToast(greetings[mRandom.nextInt(greetings.length)]); 53 } 54 showMenuFragment()55 private void showMenuFragment() { 56 if (mMenuFragment == null) { 57 mMenuFragment = new RotaryMenu(); 58 } 59 getSupportFragmentManager().beginTransaction() 60 .replace(R.id.rotary_menu, mMenuFragment) 61 .commit(); 62 } 63 showToast(String message)64 private void showToast(String message) { 65 Log.d(TAG, "showToast -> " + message); 66 Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 67 } 68 }