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 
20 import android.app.ActivityManager;
21 import android.os.Bundle;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ArrayAdapter;
27 import android.widget.Button;
28 import android.widget.LinearLayout;
29 import android.widget.ListView;
30 import android.widget.ScrollView;
31 import android.widget.Toast;
32 
33 import androidx.annotation.Nullable;
34 import androidx.fragment.app.DialogFragment;
35 import androidx.lifecycle.ViewModelProvider;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Optional;
40 
41 /**
42  * Shows a dialog with an activity name and a list of intent flags.
43  */
44 public class IntentDialogFragment extends DialogFragment {
45     private List<String> mFlags;
46     private String mActivityName;
47     private int mTaskId;
48     private static final String ARGUMENT_ACTIVITY_NAME = "activityName";
49     private static final String ARGUMENT_FLAGS = "flags";
50     private static final String TASK_ID = "taskId";
51     private static final String TAG = "IntentDialogFragment";
52 
53     /**
54      * Creates a new IntentDialogFragment to display the given flags.
55      *
56      * @param activityName The name of the activity, also the title of the dialog.
57      * @param flags        The list of flags to be displayed.
58      * @return A new IntentDialogFragment.
59      */
newInstance(String activityName, List<String> flags, int taskId)60     public static IntentDialogFragment newInstance(String activityName, List<String> flags,
61             int taskId) {
62         IntentDialogFragment fragment = new IntentDialogFragment();
63         Bundle args = new Bundle();
64         args.putString(ARGUMENT_ACTIVITY_NAME, activityName);
65         args.putStringArrayList(ARGUMENT_FLAGS, new ArrayList<>(flags));
66         args.putInt(TASK_ID, taskId);
67         fragment.setArguments(args);
68         return fragment;
69     }
70 
71     @Override
onCreate(Bundle savedInstanceState)72     public void onCreate(Bundle savedInstanceState) {
73         super.onCreate(savedInstanceState);
74         Bundle args = getArguments();
75         mFlags = args.getStringArrayList(ARGUMENT_FLAGS);
76         mActivityName = args.getString(ARGUMENT_ACTIVITY_NAME);
77         mTaskId = args.getInt(TASK_ID);
78     }
79 
80     @Nullable
81     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)82     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
83             Bundle savedInstanceState) {
84         getDialog().setTitle(mActivityName + getString(R.string.dialog_intent_flags));
85         LinearLayout rootLayout = (LinearLayout) inflater
86                 .inflate(R.layout.fragment_intent_dialog, container, false /* attachToRoot */);
87         ListView flagsListView = rootLayout.findViewById(R.id.flag_list);
88         ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
89                 R.layout.dialog_list_item, R.id.item, mFlags);
90         flagsListView.setAdapter(adapter);
91         rootLayout.findViewById(R.id.dialog_cancel).setOnClickListener(view -> {
92             getDialog().dismiss();
93         });
94 
95         Button bringToFront = rootLayout.findViewById(R.id.move_task_to_front_button);
96         bringToFront.setOnClickListener(v -> {
97             moveTaskToFront(mTaskId);
98             getDialog().dismiss();
99         });
100 
101         Button removeTask = rootLayout.findViewById(R.id.kill_task_button);
102         removeTask.setOnClickListener(v -> {
103             removeTask(mTaskId);
104             getDialog().dismiss();
105         });
106 
107         Button copyFlagsButton = rootLayout.findViewById(R.id.copy_flags_button);
108         if (mFlags.get(0).equals("None")) {
109             copyFlagsButton.setEnabled(false);
110         } else {
111             copyFlagsButton.setOnClickListener(view -> {
112                 IntentBuilderView intentBuilderView = getActivity()
113                         .findViewById(R.id.root_container)
114                         .findViewWithTag(BaseActivity.BUILDER_VIEW);
115                 intentBuilderView.selectFlags(mFlags);
116                 getDialog().dismiss();
117                 ((ScrollView) getActivity().findViewById(R.id.scroll_container)).smoothScrollTo(0,
118                         Float.valueOf(intentBuilderView.getY()).intValue());
119             });
120         }
121         return rootLayout;
122     }
123 
removeTask(int taskId)124     private void removeTask(int taskId) {
125         ActivityManager am = getActivity().getSystemService(ActivityManager.class);
126         List<ActivityManager.AppTask> appTasks = am.getAppTasks();
127 
128         Optional<ActivityManager.AppTask> taskToKill = appTasks.stream().filter(
129                 task -> task.getTaskInfo().persistentId == taskId)
130                 .findFirst();
131 
132         if (taskToKill.isPresent()) {
133             taskToKill.get().finishAndRemoveTask();
134         } else {
135             String errorMessage = "Task: " + taskId + " not found in recents, can't kill";
136             Log.e(TAG, errorMessage);
137             Toast.makeText(getContext(), errorMessage,
138                     Toast.LENGTH_SHORT).show();
139         }
140     }
141 
moveTaskToFront(int taskId)142     private void moveTaskToFront(int taskId) {
143         ActivityManager am = getActivity().getSystemService(ActivityManager.class);
144         am.moveTaskToFront(taskId, 0 /* flags */);
145     }
146 }
147