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.example.android.intentplayground;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.LinearLayout;
25 import android.widget.TextView;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.fragment.app.Fragment;
30 import androidx.lifecycle.ViewModelProvider;
31 import androidx.recyclerview.widget.LinearLayoutManager;
32 import androidx.recyclerview.widget.RecyclerView;
33 
34 import java.util.List;
35 
36 /**
37  * This fragment displays a hierarchy of tasks and activities in an expandable list.
38  */
39 public class TreeFragment extends Fragment {
40     public static final String TREE_NODE = "com.example.android.NODE_TREE";
41     public static final String FRAGMENT_TITLE = "com.example.android.TREE_FRAGMENT_TITLE";
42     private Activity mActivity;
43     private String mTitle;
44     private ViewGroup mContainer;
45     private BaseActivityViewModel mViewModel;
46 
47 
48     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)49     public View onCreateView(LayoutInflater inflater, ViewGroup container,
50             Bundle savedInstanceState) {
51         Bundle args = getArguments();
52         if (args != null) {
53             mTitle = args.getString(FRAGMENT_TITLE);
54         }
55         return inflater.inflate(R.layout.fragment_tree, container, false /* attachToRoot */);
56     }
57 
58     @Override
onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)59     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
60         super.onViewCreated(view, savedInstanceState);
61         mViewModel = (new ViewModelProvider(getActivity(),
62                 new ViewModelProvider.NewInstanceFactory())).get(BaseActivityViewModel.class);
63 
64         mViewModel.getRefresh().observe(this, this::onResumeHelper);
65 
66     }
67 
68     @Override
onResume()69     public void onResume() {
70         super.onResume();
71         mViewModel.actOnFab(BaseActivityViewModel.FabAction.Show);
72     }
73 
onResumeHelper(List<Tracking.Task> tasks)74     private void onResumeHelper(List<Tracking.Task> tasks) {
75         mActivity = getActivity();
76         LinearLayout treeLayout = (LinearLayout) getView();
77 
78         TextView titleView = treeLayout.findViewById(R.id.task_tree_title);
79         RecyclerView recyclerView = treeLayout.findViewById(R.id.tree_recycler);
80         if (mTitle != null) {
81             titleView.setText(mTitle);
82         }
83         displayRecycler(tasks, recyclerView);
84     }
85 
displayRecycler(List<Tracking.Task> tasks, RecyclerView recyclerView)86     private void displayRecycler(List<Tracking.Task> tasks, RecyclerView recyclerView) {
87         recyclerView.setHasFixedSize(true);
88         recyclerView.setNestedScrollingEnabled(false);
89         InlineAdapter adapter = new InlineAdapter(tasks, getActivity());
90         recyclerView.setAdapter(adapter);
91         recyclerView.setLayoutManager(
92                 new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
93     }
94 
95     /**
96      * Expand a task group to show its child activities.
97      *
98      * @param i The index of the task to expand.
99      */
openTask(int i)100     public void openTask(int i) {
101         View taskView = mContainer.getChildAt(i);
102         if (taskView != null) {
103             taskView.callOnClick();
104         }
105     }
106 }
107 
108