IntentPlayground start activities for result

Test: Manual
Change-Id: I3004e6dd76c026e41d2c9a28f4dd59664f99cd09
This commit is contained in:
Liam Clark
2019-01-07 11:28:23 -08:00
parent 5e9e4f94aa
commit ba9b6ca212
8 changed files with 70 additions and 88 deletions

View File

@@ -17,23 +17,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.intentplayground"> package="com.example.android.intentplayground">
<!-- Used to reorder tasks using android.app.ActivityManager.moveTaskToFront() --> <!-- Used to reorder tasks using android.app.ActivityManager.moveTaskToFront() -->
<uses-permission android:name="android.permission.REORDER_TASKS" /> <uses-permission android:name="android.permission.REORDER_TASKS"/>
<application <application
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity <activity android:name=".RegularActivity">
android:name=".LauncherActivity"
android:launchMode="singleInstance"
android:documentLaunchMode="always">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".RegularActivity" />
<activity <activity
android:name=".SingleTopActivity" android:name=".SingleTopActivity"
android:launchMode="singleTop" /> android:launchMode="singleTop" />

View File

@@ -28,13 +28,13 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/build_intent" android:text="@string/build_intent"
android:textAppearance="@style/title" /> android:textAppearance="@style/title"/>
<TextView <TextView
android:id="@+id/flags_hint" android:id="@+id/flags_hint"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/build_intent_caption" /> android:text="@string/build_intent_caption"/>
<LinearLayout <LinearLayout
android:id="@+id/suggest_list_item" android:id="@+id/suggest_list_item"
@@ -48,19 +48,19 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
android:layout_weight="1" > android:layout_weight="1">
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/suggest_enable_text" android:text="@string/suggest_enable_text"
android:textAppearance="@style/normal" /> android:textAppearance="@style/normal"/>
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/suggest_description_text" android:text="@string/suggest_description_text"
android:textAppearance="@style/caption" /> android:textAppearance="@style/caption"/>
</LinearLayout> </LinearLayout>
<Switch <Switch
@@ -80,19 +80,54 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/dividerHeight" android:layout_height="@dimen/dividerHeight"
android:layout_marginVertical="@dimen/smallMargin" android:layout_marginVertical="@dimen/smallMargin"
android:background="@drawable/divider" /> android:background="@drawable/divider"/>
<TextView <TextView
android:id="@+id/textView" android:id="@+id/textView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginVertical="@dimen/smallMargin" android:layout_marginVertical="@dimen/smallMargin"
android:text="@string/target_activity" /> android:text="@string/target_activity"/>
<RadioGroup <RadioGroup
android:id="@+id/radioGroup_launchMode" android:id="@+id/radioGroup_launchMode"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:checkedButton="@+id/radio_launchMode_standard" android:checkedButton="@+id/radio_launchMode_standard"
android:orientation="vertical" /> android:orientation="vertical"/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dividerHeight"
android:layout_marginVertical="@dimen/smallMargin"
android:background="@drawable/divider"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="@dimen/smallMargin"
android:text="@string/start_mode"/>
<RadioGroup
android:id="@+id/radioGroup_startMode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/radio_launchMode_standard"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/fullMargin"
android:id="@+id/start_normal"
android:text="@string/start_normal"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/fullMargin"
android:id="@+id/start_for_result"
android:text="@string/start_for_result"/>
</RadioGroup>
</LinearLayout> </LinearLayout>

View File

@@ -89,4 +89,7 @@
<string name="new_task">NEW</string> <string name="new_task">NEW</string>
<string name="run_intent_tests">Run Intent tests</string> <string name="run_intent_tests">Run Intent tests</string>
<string name="launch_default_activities">Launch Default Activities</string> <string name="launch_default_activities">Launch Default Activities</string>
<string name="start_mode">Select start mode</string>
<string name="start_normal">Start activity</string>
<string name="start_for_result">Start activity for result</string>
</resources> </resources>

View File

@@ -50,6 +50,7 @@ public abstract class BaseActivity extends AppCompatActivity implements
public static final String TREE_FRAGMENT = "com.example.android.treeFragment"; public static final String TREE_FRAGMENT = "com.example.android.treeFragment";
public static final String EXPECTED_TREE_FRAGMENT = "com.example.android.expectedTreeFragment"; public static final String EXPECTED_TREE_FRAGMENT = "com.example.android.expectedTreeFragment";
public static final int LAUNCH_REQUEST_CODE = 0xEF; public static final int LAUNCH_REQUEST_CODE = 0xEF;
private static final int LAUNCH_FOR_RESULT_ID = 1;
public enum Mode {LAUNCH, VERIFY, RESULT} public enum Mode {LAUNCH, VERIFY, RESULT}
@@ -155,10 +156,16 @@ public abstract class BaseActivity extends AppCompatActivity implements
/** /**
* Launches activity with the selected options. * Launches activity with the selected options.
*/ */
public void launchActivity(Intent intent) { @Override
public void launchActivity(Intent intent, boolean forResult) {
if (forResult) {
startActivityForResult(intent, LAUNCH_FOR_RESULT_ID);
} else {
startActivity(intent);
}
// If people press back we want them to see the overview rather than the launch fragment. // If people press back we want them to see the overview rather than the launch fragment.
// To achieve this we pop the launchFragment from the stack when we go to the next activity. // To achieve this we pop the launchFragment from the stack when we go to the next activity.
startActivity(intent);
getSupportFragmentManager().popBackStack(); getSupportFragmentManager().popBackStack();
} }

View File

@@ -215,6 +215,11 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
} }
public boolean startForResult() {
RadioButton startNormal = mLayout.findViewById(R.id.start_normal);
return !startNormal.isChecked();
}
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
int buttonId = compoundButton.getId(); int buttonId = compoundButton.getId();
@@ -358,6 +363,6 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
* button within this view. * button within this view.
*/ */
public interface OnLaunchCallback { public interface OnLaunchCallback {
void launchActivity(Intent intent); void launchActivity(Intent intent, boolean forResult);
} }
} }

View File

@@ -16,6 +16,7 @@
package com.example.android.intentplayground; package com.example.android.intentplayground;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@@ -74,7 +75,9 @@ public class LaunchFragment extends Fragment {
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.app_bar_launch && mOnLaunchCallback != null) { if (item.getItemId() == R.id.app_bar_launch && mOnLaunchCallback != null) {
mOnLaunchCallback.launchActivity(mIntentBuilderView.currentIntent()); Intent intent = mIntentBuilderView.currentIntent();
boolean forResult = mIntentBuilderView.startForResult();
mOnLaunchCallback.launchActivity(mIntentBuilderView.currentIntent(), forResult);
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);

View File

@@ -1,57 +0,0 @@
/*
* Copyright (C) 2017 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.content.Intent;
import android.os.Bundle;
import android.view.View;
/**
* A singleInstance activity that is responsible for a launching a bootstrap stack of activities.
*/
public class LauncherActivity extends BaseActivity {
public static final String TAG = "LauncherActivity";
private static final long SNACKBAR_DELAY = 75;
private TestBase mTester;
private boolean mFirstLaunch;
private View mSnackBarRootView;
private boolean mSnackBarIsVisible = false;
private boolean mDontLaunch = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupTaskPreset();
mFirstLaunch = true;
}
/**
* Launches activity with the selected options.
*/
@Override
public void launchActivity(Intent intent) {
startActivityForResult(intent, LAUNCH_REQUEST_CODE);
// If people press back we want them to see the overview rather than the launch fragment.
// To achieve this we pop the launchFragment from the stack when we go to the next activity.
getSupportFragmentManager().popBackStack();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
}

View File

@@ -80,7 +80,7 @@ public class TestBase {
tsb.startActivities(); tsb.startActivities();
Thread.sleep(500); Thread.sleep(500);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
Log.e(LauncherActivity.TAG, ie.getMessage()); Log.e(TAG, ie.getMessage());
} }
}); });
break; break;
@@ -91,7 +91,7 @@ public class TestBase {
try { try {
Thread.sleep(500); Thread.sleep(500);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
Log.e(LauncherActivity.TAG, ie.getMessage()); Log.e(TAG, ie.getMessage());
} }
ArrayList<Intent> nextIntents = new ArrayList<>(Arrays.asList( ArrayList<Intent> nextIntents = new ArrayList<>(Arrays.asList(
tsb.getIntents())); tsb.getIntents()));
@@ -348,16 +348,6 @@ public class TestBase {
} }
} }
public static void clearRunningTasks(Context context) {
ComponentName launcher = new ComponentName(context, LauncherActivity.class);
context.getSystemService(ActivityManager.class).getAppTasks().stream()
.filter(task -> {
ActivityManager.RecentTaskInfo info = task.getTaskInfo();
return (info.baseActivity != null) && (!info.baseActivity.equals(launcher));
})
.forEach(ActivityManager.AppTask::finishAndRemoveTask);
}
public Context getContext() { return mContext; } public Context getContext() { return mContext; }
/** /**