mirror of
https://github.com/oasisfeng/deagle.git
synced 2025-01-09 04:10:03 +08:00
ADD: Utility class Bundles for convenient Bundle building.
This commit is contained in:
parent
1e13d9f5c6
commit
5ebc931b3a
38
library/src/main/java/com/oasisfeng/android/os/Bundles.java
Normal file
38
library/src/main/java/com/oasisfeng/android/os/Bundles.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.oasisfeng.android.os;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.oasisfeng.android.util.Consumer;
|
||||
|
||||
/**
|
||||
* Bundle-related helpers
|
||||
*
|
||||
* Created by Oasis on 2016/10/4.
|
||||
*/
|
||||
public class Bundles {
|
||||
|
||||
@SafeVarargs public static Bundle build(final Consumer<Bundle>... actions) {
|
||||
final Bundle bundle = new Bundle(actions.length);
|
||||
for (final Consumer<Bundle> action : actions)
|
||||
action.accept(bundle);
|
||||
return bundle;
|
||||
}
|
||||
|
||||
/** Make a Bundle for a single key/value pair. */
|
||||
public static Bundle forPair(final String key, final String value) {
|
||||
final Bundle b = new Bundle(1);
|
||||
b.putString(key, value);
|
||||
return b;
|
||||
}
|
||||
|
||||
public static Bundle forPair(final String key, final Parcelable value) {
|
||||
final Bundle b = new Bundle(1);
|
||||
b.putParcelable(key, value);
|
||||
return b;
|
||||
}
|
||||
|
||||
public static Bundle of(final Parcelable value) { return forPair(null, value); }
|
||||
|
||||
private Bundles() {}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.oasisfeng.android.util;
|
||||
|
||||
/**
|
||||
* Represents an operation that accepts a single input argument and returns no
|
||||
* result. Unlike most other functional interfaces, {@code Consumer} is expected
|
||||
* to operate via side-effects.
|
||||
*
|
||||
* @param <T> the type of the input to the operation
|
||||
*/
|
||||
public interface Consumer<T> {
|
||||
|
||||
/**
|
||||
* Performs this operation on the given argument.
|
||||
*
|
||||
* @param t the input argument
|
||||
*/
|
||||
void accept(T t);
|
||||
}
|
@ -23,10 +23,6 @@ import java.util.concurrent.ThreadPoolExecutor;
|
||||
*/
|
||||
public abstract class SafeAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
|
||||
|
||||
public interface Consumer<T> {
|
||||
void accept(T object);
|
||||
}
|
||||
|
||||
public static void execute(final Activity activity, final Consumer<Activity> runnable) {
|
||||
final WeakReference<Activity> reference = new WeakReference<>(activity);
|
||||
AsyncTask.execute(new SafeTask<>(new Supplier<Activity>() { @Override public Activity get() {
|
||||
|
Loading…
Reference in New Issue
Block a user