|
| 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 | + */ |
1 | 14 | package rx.android.content;
|
2 | 15 |
|
3 |
| -import android.app.Activity; |
4 |
| -import android.app.Fragment; |
5 | 16 | import android.content.Context;
|
6 | 17 | import android.content.Intent;
|
7 | 18 | import android.content.IntentFilter;
|
8 | 19 | import android.content.SharedPreferences;
|
9 | 20 | import android.database.Cursor;
|
10 |
| -import android.os.Build; |
11 | 21 | import android.os.Handler;
|
12 | 22 |
|
13 | 23 | import rx.Observable;
|
14 |
| -import rx.android.internal.Assertions; |
15 |
| -import rx.functions.Func1; |
16 |
| - |
17 |
| -import static rx.android.schedulers.AndroidSchedulers.mainThread; |
18 | 24 |
|
19 | 25 | public final class ContentObservable {
|
20 | 26 | private ContentObservable() {
|
21 | 27 | throw new AssertionError("No instances");
|
22 | 28 | }
|
23 | 29 |
|
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 |
| - |
104 | 30 | /**
|
105 | 31 | * Create Observable that wraps BroadcastReceiver and emmit received intents.
|
106 | 32 | *
|
|
0 commit comments