Skip to content

Commit 982256a

Browse files
committed
Merge pull request ReactiveX#92 from omo/content-to-app
Move Activity and Fragment related operations to rx.android.app
2 parents 1ae5e5c + 601e9d1 commit 982256a

11 files changed

+293
-231
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package rx.android.app;
15+
16+
import android.app.Activity;
17+
import android.app.Fragment;
18+
import android.os.Build;
19+
20+
import rx.Observable;
21+
import rx.android.internal.Assertions;
22+
import rx.functions.Func1;
23+
24+
import static rx.android.schedulers.AndroidSchedulers.mainThread;
25+
26+
public final class AppObservable {
27+
private AppObservable() {
28+
throw new AssertionError("No instances");
29+
}
30+
31+
static {
32+
boolean supportFragmentsAvailable = false;
33+
try {
34+
Class.forName("android.support.v4.app.Fragment");
35+
supportFragmentsAvailable = true;
36+
} catch (ClassNotFoundException e) {
37+
}
38+
39+
USES_SUPPORT_FRAGMENTS = supportFragmentsAvailable;
40+
}
41+
42+
private static final Func1<Activity, Boolean> ACTIVITY_VALIDATOR = new Func1<Activity, Boolean>() {
43+
@Override
44+
public Boolean call(Activity activity) {
45+
return !activity.isFinishing();
46+
}
47+
};
48+
private static final Func1<Fragment, Boolean> FRAGMENT_VALIDATOR = new Func1<Fragment, Boolean>() {
49+
@Override
50+
public Boolean call(Fragment fragment) {
51+
return fragment.isAdded() && !fragment.getActivity().isFinishing();
52+
}
53+
};
54+
private static final Func1<android.support.v4.app.Fragment, Boolean> FRAGMENTV4_VALIDATOR =
55+
new Func1<android.support.v4.app.Fragment, Boolean>() {
56+
@Override
57+
public Boolean call(android.support.v4.app.Fragment fragment) {
58+
return fragment.isAdded() && !fragment.getActivity().isFinishing();
59+
}
60+
};
61+
public static final boolean USES_SUPPORT_FRAGMENTS;
62+
63+
/**
64+
* Binds the given source sequence to an activity.
65+
* <p>
66+
* This helper will schedule the given sequence to be observed on the main UI thread and ensure
67+
* that no notifications will be forwarded to the activity in case it is scheduled to finish.
68+
* <p>
69+
* You should unsubscribe from the returned Observable in onDestroy at the latest, in order to not
70+
* leak the activity or an inner subscriber. Conversely, when the source sequence can outlive the activity,
71+
* make sure to bind to new instances of the activity again, e.g. after going through configuration changes.
72+
* Refer to the samples project for actual examples.
73+
*
74+
* @param activity the activity to bind the source sequence to
75+
* @param source the source sequence
76+
*/
77+
public static <T> Observable<T> bindActivity(Activity activity, Observable<T> source) {
78+
Assertions.assertUiThread();
79+
return source.observeOn(mainThread()).lift(new OperatorConditionalBinding<T, Activity>(activity, ACTIVITY_VALIDATOR));
80+
}
81+
82+
/**
83+
* Binds the given source sequence to a fragment (native or support-v4).
84+
* <p>
85+
* This helper will schedule the given sequence to be observed on the main UI thread and ensure
86+
* that no notifications will be forwarded to the fragment in case it gets detached from its
87+
* activity or the activity is scheduled to finish.
88+
* <p>
89+
* You should unsubscribe from the returned Observable in onDestroy for normal fragments, or in onDestroyView
90+
* for retained fragments, in order to not leak any references to the host activity or the fragment.
91+
* Refer to the samples project for actual examples.
92+
*
93+
* @param fragment the fragment to bind the source sequence to
94+
* @param source the source sequence
95+
*/
96+
public static <T> Observable<T> bindFragment(Object fragment, Observable<T> source) {
97+
Assertions.assertUiThread();
98+
final Observable<T> o = source.observeOn(mainThread());
99+
if (USES_SUPPORT_FRAGMENTS && fragment instanceof android.support.v4.app.Fragment) {
100+
android.support.v4.app.Fragment f = (android.support.v4.app.Fragment) fragment;
101+
return o.lift(new OperatorConditionalBinding<T, android.support.v4.app.Fragment>(f, FRAGMENTV4_VALIDATOR));
102+
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && fragment instanceof Fragment) {
103+
Fragment f = (Fragment) fragment;
104+
return o.lift(new OperatorConditionalBinding<T, Fragment>(f, FRAGMENT_VALIDATOR));
105+
} else {
106+
throw new IllegalArgumentException("Target fragment is neither a native nor support library Fragment");
107+
}
108+
}
109+
}

rxandroid/src/main/java/rx/android/content/OperatorConditionalBinding.java renamed to rxandroid/src/main/java/rx/android/app/OperatorConditionalBinding.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
* Licensed under the Apache License, Version 2.0 (the "License");
33
* you may not use this file except in compliance with the License.
44
* You may obtain a copy of the License at
5-
*
5+
*
66
* http://www.apache.org/licenses/LICENSE-2.0
7-
*
7+
*
88
* Unless required by applicable law or agreed to in writing, software
99
* distributed under the License is distributed on an "AS IS" BASIS,
1010
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
*/
14-
package rx.android.content;
14+
package rx.android.app;
1515

1616
import rx.Observable;
1717
import rx.Subscriber;

rxandroid/src/main/java/rx/android/content/ContentObservable.java

Lines changed: 13 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,32 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
114
package rx.android.content;
215

3-
import android.app.Activity;
4-
import android.app.Fragment;
516
import android.content.Context;
617
import android.content.Intent;
718
import android.content.IntentFilter;
819
import android.content.SharedPreferences;
920
import android.database.Cursor;
10-
import android.os.Build;
1121
import android.os.Handler;
1222

1323
import rx.Observable;
14-
import rx.android.internal.Assertions;
15-
import rx.functions.Func1;
16-
17-
import static rx.android.schedulers.AndroidSchedulers.mainThread;
1824

1925
public final class ContentObservable {
2026
private ContentObservable() {
2127
throw new AssertionError("No instances");
2228
}
2329

24-
private static final Func1<Activity, Boolean> ACTIVITY_VALIDATOR = new Func1<Activity, Boolean>() {
25-
@Override
26-
public Boolean call(Activity activity) {
27-
return !activity.isFinishing();
28-
}
29-
};
30-
private static final Func1<Fragment, Boolean> FRAGMENT_VALIDATOR = new Func1<Fragment, Boolean>() {
31-
@Override
32-
public Boolean call(Fragment fragment) {
33-
return fragment.isAdded() && !fragment.getActivity().isFinishing();
34-
}
35-
};
36-
private static final Func1<android.support.v4.app.Fragment, Boolean> FRAGMENTV4_VALIDATOR =
37-
new Func1<android.support.v4.app.Fragment, Boolean>() {
38-
@Override
39-
public Boolean call(android.support.v4.app.Fragment fragment) {
40-
return fragment.isAdded() && !fragment.getActivity().isFinishing();
41-
}
42-
};
43-
44-
private static final boolean USES_SUPPORT_FRAGMENTS;
45-
46-
static {
47-
boolean supportFragmentsAvailable = false;
48-
try {
49-
Class.forName("android.support.v4.app.Fragment");
50-
supportFragmentsAvailable = true;
51-
} catch (ClassNotFoundException e) {
52-
}
53-
54-
USES_SUPPORT_FRAGMENTS = supportFragmentsAvailable;
55-
}
56-
57-
/**
58-
* Binds the given source sequence to an activity.
59-
* <p>
60-
* This helper will schedule the given sequence to be observed on the main UI thread and ensure
61-
* that no notifications will be forwarded to the activity in case it is scheduled to finish.
62-
* <p>
63-
* You should unsubscribe from the returned Observable in onDestroy at the latest, in order to not
64-
* leak the activity or an inner subscriber. Conversely, when the source sequence can outlive the activity,
65-
* make sure to bind to new instances of the activity again, e.g. after going through configuration changes.
66-
* Refer to the samples project for actual examples.
67-
*
68-
* @param activity the activity to bind the source sequence to
69-
* @param source the source sequence
70-
*/
71-
public static <T> Observable<T> bindActivity(Activity activity, Observable<T> source) {
72-
Assertions.assertUiThread();
73-
return source.observeOn(mainThread()).lift(new OperatorConditionalBinding<T, Activity>(activity, ACTIVITY_VALIDATOR));
74-
}
75-
76-
/**
77-
* Binds the given source sequence to a fragment (native or support-v4).
78-
* <p>
79-
* This helper will schedule the given sequence to be observed on the main UI thread and ensure
80-
* that no notifications will be forwarded to the fragment in case it gets detached from its
81-
* activity or the activity is scheduled to finish.
82-
* <p>
83-
* You should unsubscribe from the returned Observable in onDestroy for normal fragments, or in onDestroyView
84-
* for retained fragments, in order to not leak any references to the host activity or the fragment.
85-
* Refer to the samples project for actual examples.
86-
*
87-
* @param fragment the fragment to bind the source sequence to
88-
* @param source the source sequence
89-
*/
90-
public static <T> Observable<T> bindFragment(Object fragment, Observable<T> source) {
91-
Assertions.assertUiThread();
92-
final Observable<T> o = source.observeOn(mainThread());
93-
if (USES_SUPPORT_FRAGMENTS && fragment instanceof android.support.v4.app.Fragment) {
94-
android.support.v4.app.Fragment f = (android.support.v4.app.Fragment) fragment;
95-
return o.lift(new OperatorConditionalBinding<T, android.support.v4.app.Fragment>(f, FRAGMENTV4_VALIDATOR));
96-
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && fragment instanceof Fragment) {
97-
Fragment f = (Fragment) fragment;
98-
return o.lift(new OperatorConditionalBinding<T, Fragment>(f, FRAGMENT_VALIDATOR));
99-
} else {
100-
throw new IllegalArgumentException("Target fragment is neither a native nor support library Fragment");
101-
}
102-
}
103-
10430
/**
10531
* Create Observable that wraps BroadcastReceiver and emmit received intents.
10632
*

rxandroid/src/main/java/rx/android/view/ViewObservable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static Observable<OnClickEvent> clicks(final View view, final boolean emi
4040
* This helper will schedule the given sequence to be observed on the main UI thread and ensure
4141
* that no notifications will be forwarded to the view in case it gets detached from its the window.
4242
* <p>
43-
* Unlike {@link rx.android.content.ContentObservable#bindActivity} or {@link rx.android.content.ContentObservable#bindFragment}, you don't have to unsubscribe the returned {@code Observable}
43+
* Unlike {@link rx.android.app.AppObservable#bindActivity} or {@link rx.android.app.AppObservable#bindFragment}, you don't have to unsubscribe the returned {@code Observable}
4444
* on the detachment. {@link #bindView} does it automatically.
4545
* That means that the subscriber doesn't see further sequence even if the view is recycled and
4646
* attached again.

0 commit comments

Comments
 (0)