Move the launch activity button to the toolbar

Test: Manual
Change-Id: I81155492b39d6c72b0976a738a90ed00226bc1e8
This commit is contained in:
Liam Clark
2018-11-27 10:54:27 -08:00
parent b2579ca8d4
commit 64a9fe4304
8 changed files with 112 additions and 157 deletions

View File

@@ -26,7 +26,6 @@ import android.support.design.widget.FloatingActionButton;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.appcompat.app.AlertDialog;
@@ -153,13 +152,10 @@ public abstract class BaseActivity extends AppCompatActivity implements
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.app_bar_help:
showHelpDialog();
break;
case R.id.app_bar_test:
runIntentTests();
break;
case R.id.app_bar_launch:
case R.id.app_bar_launch_default:
askToLaunchTasks();
break;
}
@@ -215,37 +211,6 @@ public abstract class BaseActivity extends AppCompatActivity implements
.getLaunchIntentForPackage("com.example.android.intentplayground.test"));
}
/**
* Creates and displays a help overlay on this activity.
*/
protected void showHelpDialog() {
FragmentManager fragmentManager = getSupportFragmentManager();
LinearLayout container = findViewById(R.id.fragment_container);
container.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
ShowcaseFragment demo = new ShowcaseFragment();
demo.addStep(R.string.help_step_one, R.id.task_tree_container, () -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
TreeFragment frag = (TreeFragment) fragmentManager.findFragmentByTag(TREE_FRAGMENT);
if (frag != null) {
frag.openTask(0);
frag.openTask(1);
}
}
});
demo.addStep(R.string.help_step_two, R.id.intent_container);
demo.addStep(R.string.help_step_three, R.id.build_intent_container,
R.id.build_intent_view);
demo.addStep(R.string.help_step_four, R.id.fragment_container_bottom,
R.id.launch_button);
demo.setScroller(findViewById(R.id.scroll_container));
demo.setOnFinish(() -> container.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE));
fragmentManager.beginTransaction()
.add(R.id.root_container, demo)
.addToBackStack(null)
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.commit();
}
protected Intent prepareLaunchForward() {
Intent intent = getIntent();
Intent nextIntent = null;

View File

@@ -1,81 +0,0 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.intentplayground;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.AppTask;
import android.app.ActivityManager.RecentTaskInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.util.List;
import java.util.Locale;
/**
* Displays details about the current task and activity.
*/
public class CurrentTaskFragment extends Fragment {
private TextView mCurrentTaskView, mCurrentActivityView, mLastTaskView, mLastActivityView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
Bundle savedInstanceState) {
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.fragment_current_task,
container, false /* attachToRoot */);
mCurrentTaskView = layout.findViewById(R.id.current_task);
mCurrentActivityView = layout.findViewById(R.id.current_activity);
mLastTaskView = layout.findViewById(R.id.last_task);
mLastActivityView = layout.findViewById(R.id.last_activity);
return layout;
}
@Override
public void onResume() {
super.onResume();
Activity activity = getActivity();
Resources res = activity.getResources();
List<AppTask> tasks = activity.getSystemService(ActivityManager.class).getAppTasks();
RecentTaskInfo currentTask = tasks.get(0).getTaskInfo();
RecentTaskInfo lastTask = tasks.size() > 1 && tasks.get(1) != null ?
tasks.get(1).getTaskInfo() : null;
mCurrentTaskView.setText(String.format(Locale.ENGLISH, "#%d", currentTask.persistentId));
mCurrentTaskView.setTextColor(res.getColor(ColorManager.getColorForTask(
currentTask.persistentId), null /* theme */));
mCurrentActivityView.setText(currentTask.topActivity.getShortClassName());
mCurrentActivityView.setTextColor(res.getColor(ColorManager.getColorForActivity(
currentTask.topActivity), null /* theme */));
if (lastTask != null) {
mLastTaskView.setText(String.format(Locale.ENGLISH, "#%d", lastTask.persistentId));
mLastTaskView.setTextColor(res.getColor(ColorManager.getColorForTask(
lastTask.persistentId), null /* theme */));
if (lastTask.topActivity != null) {
mLastActivityView.setText(lastTask.topActivity.getShortClassName());
mLastActivityView.setTextColor(res.getColor(ColorManager.getColorForActivity(
lastTask.topActivity), null /* theme */));
}
}
}
}

View File

@@ -27,7 +27,6 @@ import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
@@ -38,6 +37,7 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
@@ -66,10 +66,11 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
/**
* Constructs a new IntentBuilderView, in the specified mode.
*
* @param context The context of the activity that holds this view.
* @param mode The mode to launch in (if null, default mode turns suggestions off). Passing
* {@link com.example.android.intentplayground.BaseActivity.Mode} will turn on suggestions
* by default.
* @param mode The mode to launch in (if null, default mode turns suggestions off). Passing
* {@link BaseActivity.Mode} will turn on suggestions
* by default.
*/
public IntentBuilderView(@NonNull Context context, BaseActivity.Mode mode) {
super(context);
@@ -95,7 +96,7 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
String fullName = mContext.getPackageName().concat(".").concat(name);
try {
return Class.forName(fullName);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException e) {
if (BuildConfig.DEBUG) e.printStackTrace();
throw new RuntimeException(e);
}
@@ -138,22 +139,22 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
rb.setOnClickListener(this);
activityRadios.addView(actRadio);
});
mLayout.findViewById(R.id.launch_button).setOnClickListener(this);
((CompoundButton) mLayout.findViewById(R.id.suggestion_switch))
.setOnCheckedChangeListener(this);
}
/**
* Fills the {@link ViewGroup} with a list separated by section
* @param layout The layout to fill
* @param categories A map of category names to list items within that category
*
* @param layout The layout to fill
* @param categories A map of category names to list items within that category
* @param categoryLayoutRes the layout resource of the category header view
* @param categoryViewId the resource id of the category {@link TextView} within the layout
* @param itemLayoutRes the layout resource of the list item view
* @param itemViewId the resource id of the item {@link TextView} within the item layout
* @param categoryViewId the resource id of the category {@link TextView} within the layout
* @param itemLayoutRes the layout resource of the list item view
* @param itemViewId the resource id of the item {@link TextView} within the item layout
*/
private void fillCheckBoxLayout(ViewGroup layout, Map<String, List<String>> categories,
int categoryLayoutRes, int categoryViewId, int itemLayoutRes,int itemViewId) {
int categoryLayoutRes, int categoryViewId, int itemLayoutRes, int itemViewId) {
layout.removeAllViews();
for (String category : categories.keySet()) {
View categoryLayout = mInflater.inflate(categoryLayoutRes, layout,
@@ -181,13 +182,10 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
ActivityInfo tag = (ActivityInfo) view.getTag();
mActivityToLaunch = new ComponentName(mContext,
getClass(tag.name.substring(tag.name.lastIndexOf(".") + 1)));
} else if (view instanceof Button && view.getId() == R.id.launch_button) {
// Handles click on Launch Button
mLaunchCallback.launchActivity(currentIntent());
}
}
private Intent currentIntent() {
public Intent currentIntent() {
LinearLayout flagBuilder = mLayout.findViewById(R.id.build_intent_flags);
Intent intent = new Intent();
// Gather flags from flag builder checkbox list
@@ -204,9 +202,6 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
return intent;
}
public void setOnLaunchCallback(OnLaunchCallback listener) {
mLaunchCallback = listener;
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@@ -230,7 +225,9 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
if (mVerifyMode) {
refreshConstraints();
getCheckedFlags().forEach(this::suggestFlags);
} else enableAllFlags();
} else {
enableAllFlags();
}
}
}
@@ -244,11 +241,11 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
List<String> suggestions = flag.getComplements().stream().map(IntentFlag::getName)
.collect(Collectors.toList());
getAllCheckBoxes().stream()
.filter(box -> hasSuggestion(suggestions, box))
.forEach(box -> {
box.setButtonTintList(mSuggestTint);
box.setTag(TAG_SUGGESTED, true);
});
.filter(box -> hasSuggestion(suggestions, box))
.forEach(box -> {
box.setButtonTintList(mSuggestTint);
box.setTag(TAG_SUGGESTED, true);
});
}
private boolean hasSuggestion(List<String> suggestions, CheckBox box) {
@@ -287,6 +284,7 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
/**
* Retrieve children of a certain type from a {@link ViewGroup}.
*
* @param group the ViewGroup to retrieve children from.
*/
protected static <T> List<T> childrenOfGroup(ViewGroup group, Class<T> viewType) {
@@ -300,6 +298,7 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
/**
* Selects the checkboxes for the given list of flags.
*
* @param flags A list of mIntent flags to select.
*/
public void selectFlags(List<String> flags) {
@@ -309,8 +308,10 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
}
});
}
/**
* Selects the checkboxes for the given list of flags.
*
* @param flags A list of mIntent flags to select.
*/
public void selectFlags(Collection<IntentFlag> flags) {

View File

@@ -1,7 +1,26 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.intentplayground;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@@ -17,17 +36,22 @@ import com.example.android.intentplayground.IntentBuilderView.OnLaunchCallback;
public class LaunchFragment extends Fragment {
private IntentBuilderView mIntentBuilderView;
private BaseActivityViewModel mViewModel;
private OnLaunchCallback mOnLaunchCallback;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
mIntentBuilderView = new IntentBuilderView(getContext(), Mode.LAUNCH);
setOnLaunchCallBack();
FragmentActivity activity = getActivity();
if (activity instanceof OnLaunchCallback) {
mOnLaunchCallback = (OnLaunchCallback) activity;
}
setHasOptionsMenu(true);
return mIntentBuilderView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
mViewModel = (new ViewModelProvider(getActivity(),
@@ -40,10 +64,19 @@ public class LaunchFragment extends Fragment {
mViewModel.actOnFab(BaseActivityViewModel.FabAction.Hide);
}
private void setOnLaunchCallBack() {
FragmentActivity activity = this.getActivity();
if (activity instanceof OnLaunchCallback) {
mIntentBuilderView.setOnLaunchCallback((OnLaunchCallback) activity);
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.launch_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.app_bar_launch && mOnLaunchCallback != null) {
mOnLaunchCallback.launchActivity(mIntentBuilderView.currentIntent());
}
return super.onOptionsItemSelected(item);
}
}