Skip to content
This repository has been archived by the owner on Jan 4, 2018. It is now read-only.

Commit

Permalink
Merge pull request square#238 from square/ray/is-it-safe-211
Browse files Browse the repository at this point in the history
Fixes intent handling and adds sample app, test for it.
  • Loading branch information
rjrjr authored Apr 14, 2017
2 parents 4c6bec5 + 4dba440 commit c4730dd
Show file tree
Hide file tree
Showing 16 changed files with 331 additions and 1 deletion.
46 changes: 46 additions & 0 deletions flow-sample-intents/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
buildscript {
dependencies {
classpath deps.android_gradle_plugin
}
}

apply plugin: 'com.android.application'

android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

defaultConfig {
applicationId "flow.sample.intents"

minSdkVersion rootProject.ext.minSdkVersion
versionName VERSION_NAME
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
}
}

packagingOptions {
exclude 'LICENSE.txt'
}
}

dependencies {
compile project(':flow')
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
}

//noinspection GroovyAssignabilityCheck
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:23.1.1'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package flow.sample.basic;

import android.content.Context;
import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import flow.Flow;
import flow.History;
import flow.sample.intents.IntentsSingleInstanceSampleActivity;
import flow.sample.intents.IntentsStandardSampleActivity;
import flow.sample.intents.StringParceler;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.pressBack;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static java.util.Arrays.asList;

/**
* Demonstrates the use of Intents to drive espresso tests to specific screens.
*/
public class IntentsSampleTest {

@Rule public ActivityTestRule rule = new ActivityTestRule<>(IntentsSingleInstanceSampleActivity.class);

@Test public void singleInstanceActivity() {
List<String> expected = asList("Able", "Baker", "Charlie");
History h = History.emptyBuilder().pushAll(expected).build();

Context context = getInstrumentation().getTargetContext().getApplicationContext();
Intent intent = new Intent(context, IntentsSingleInstanceSampleActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Flow.addHistory(intent, h, new StringParceler());
context.startActivity(intent);

onView(withText("Charlie")).perform(pressBack());
onView(withText("Baker")).perform(pressBack());
onView(withText("Able")).perform(click());
}

@Test public void standardActivity() {
List<String> expected = asList("Higgledy", "Piggledy", "Pop");
History h = History.emptyBuilder().pushAll(expected).build();

Context context = getInstrumentation().getTargetContext().getApplicationContext();
Intent intent = new Intent(context, IntentsStandardSampleActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Flow.addHistory(intent, h, new StringParceler());
context.startActivity(intent);

onView(withText("Pop")).perform(pressBack());
onView(withText("Piggledy")).perform(pressBack());
onView(withText("Higgledy")).perform(click());
}
}
46 changes: 46 additions & 0 deletions flow-sample-intents/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2017 Square Inc.
~
~ 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="flow.sample.intents" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >

<activity
android:name=".IntentsSingleInstanceSampleActivity"
android:launchMode="singleInstance"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".IntentsStandardSampleActivity"
>
</activity>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2017 Square Inc.
*
* 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 flow.sample.intents;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import flow.Flow;

public class IntentsSingleInstanceSampleActivity extends Activity {

@Override protected void attachBaseContext(Context baseContext) {
baseContext = Flow.configure(baseContext, this).keyParceler(new StringParceler()).install();
super.attachBaseContext(baseContext);
}

@Override protected void onNewIntent(Intent intent) {
Flow.onNewIntent(intent, this);
}

@Override public void onBackPressed() {
if (!Flow.get(this).goBack()) {
super.onBackPressed();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2017 Square Inc.
*
* 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 flow.sample.intents;

import android.app.Activity;
import android.content.Context;
import flow.Flow;

public class IntentsStandardSampleActivity extends Activity {

@Override protected void attachBaseContext(Context baseContext) {
baseContext = Flow.configure(baseContext, this).keyParceler(new StringParceler()).install();
super.attachBaseContext(baseContext);
}

@Override public void onBackPressed() {
if (!Flow.get(this).goBack()) {
super.onBackPressed();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2017 Square Inc.
*
* 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 flow.sample.intents;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import flow.KeyParceler;

public class StringParceler implements KeyParceler {
@NonNull @Override public Parcelable toParcelable(@NonNull Object key) {
Bundle bundle = new Bundle();
bundle.putString("stringKey", (String) key);
return bundle;
}

@NonNull @Override public Object toKey(@NonNull Parcelable parcelable) {
Bundle bundle = (Bundle) parcelable;
return bundle.getString("stringKey");
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions flow-sample-intents/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2017 Square Inc.
~
~ 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.
-->

<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
19 changes: 19 additions & 0 deletions flow-sample-intents/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!--
~ Copyright 2017 Square Inc.
~
~ 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.
-->

<resources>
<string name="app_name">Flow Intents Sample</string>
</resources>
27 changes: 27 additions & 0 deletions flow-sample-intents/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
~ Copyright 2017 Square Inc.
~
~ 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.
-->

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>
3 changes: 2 additions & 1 deletion flow/src/main/java/flow/InternalLifecycleIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ static void install(final Application app, final Activity activity,
}
// We always replace the dispatcher because it frequently references the Activity.
fragment.dispatcher = dispatcher;
fragment.intent = a.getIntent();
if (newFragment) {
activity.getFragmentManager() //
.beginTransaction() //
Expand Down Expand Up @@ -111,7 +112,7 @@ static void addHistoryToIntent(Intent intent, History history, KeyParceler parce
Object key = keys.next();
parcelables.add(State.empty(key).toBundle(parceler));
}
bundle.putParcelableArrayList("FLOW_STATE", parcelables);
bundle.putParcelableArrayList(PERSISTENCE_KEY, parcelables);
intent.putExtra(INTENT_KEY, bundle);
}

Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include ':flow'
include ':flow-sample-helloworld'
include ':flow-sample-intents'
include ':flow-sample-basic'
include ':flow-sample-multikey'
include ':flow-sample-orientation-lock'
Expand Down

0 comments on commit c4730dd

Please sign in to comment.