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.example.android.intentplayground; 18 19 import android.content.Intent; 20 import android.os.Bundle; 21 import android.view.LayoutInflater; 22 import android.view.Menu; 23 import android.view.MenuInflater; 24 import android.view.MenuItem; 25 import android.view.View; 26 import android.view.ViewGroup; 27 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 import androidx.fragment.app.Fragment; 31 import androidx.fragment.app.FragmentActivity; 32 import androidx.lifecycle.ViewModelProvider; 33 34 import com.example.android.intentplayground.BaseActivity.Mode; 35 import com.example.android.intentplayground.IntentBuilderView.OnLaunchCallback; 36 37 public class LaunchFragment extends Fragment { 38 private IntentBuilderView mIntentBuilderView; 39 private BaseActivityViewModel mViewModel; 40 private OnLaunchCallback mOnLaunchCallback; 41 42 @Nullable 43 @Override onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)44 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 45 @Nullable Bundle savedInstanceState) { 46 mIntentBuilderView = new IntentBuilderView(getContext(), Mode.LAUNCH); 47 FragmentActivity activity = getActivity(); 48 if (activity instanceof OnLaunchCallback) { 49 mOnLaunchCallback = (OnLaunchCallback) activity; 50 } 51 52 setHasOptionsMenu(true); 53 return mIntentBuilderView; 54 } 55 56 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)57 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 58 mViewModel = (new ViewModelProvider(getActivity(), 59 new ViewModelProvider.NewInstanceFactory())).get(BaseActivityViewModel.class); 60 } 61 62 @Override onResume()63 public void onResume() { 64 super.onResume(); 65 mViewModel.actOnFab(BaseActivityViewModel.FabAction.Hide); 66 } 67 68 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)69 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 70 inflater.inflate(R.menu.launch_menu, menu); 71 72 super.onCreateOptionsMenu(menu, inflater); 73 } 74 75 @Override onOptionsItemSelected(MenuItem item)76 public boolean onOptionsItemSelected(MenuItem item) { 77 if (item.getItemId() == R.id.app_bar_launch && mOnLaunchCallback != null) { 78 Intent intent = mIntentBuilderView.currentIntent(); 79 boolean forResult = mIntentBuilderView.startForResult(); 80 mOnLaunchCallback.launchActivity(mIntentBuilderView.currentIntent(), forResult); 81 } 82 83 return super.onOptionsItemSelected(item); 84 } 85 } 86