Search in sources :

Example 1 with FlowableEmitter

use of io.reactivex.FlowableEmitter in project GitTest by xiaoxige.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    testView = (TestView) findViewById(R.id.testView);
    testView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            MainActivityPermissionsDispatcher.showCameraWithPermissionCheck(MainActivity.this);
        }
    });
    try {
        ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
        String appMV = appInfo.metaData.getString("MTA_CHANNEL");
        Toast.makeText(MainActivity.this, "appMV = " + appMV, Toast.LENGTH_SHORT).show();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    simpleDraweeView = (SimpleDraweeView) findViewById(R.id.simpleDraweeView);
    testView.setProgress(100, false);
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            testView.setProgress(3, true);
        }
    }, 2000);
    GenericDraweeHierarchy hierarchy = simpleDraweeView.getHierarchy();
    RoundingParams roundingParams = new RoundingParams();
    roundingParams.setBorder(Color.RED, 10);
    roundingParams.setRoundAsCircle(true);
    hierarchy.setRoundingParams(roundingParams);
    simpleDraweeView.setHierarchy(hierarchy);
    ControllerListener listener = new BaseControllerListener() {

        @Override
        public void onFinalImageSet(String id, @Nullable Object imageInfo, @Nullable Animatable animatable) {
            super.onFinalImageSet(id, imageInfo, animatable);
        }
    };
    DraweeController controller = Fresco.newDraweeControllerBuilder().setUri(new Uri.Builder().scheme(UriUtil.LOCAL_RESOURCE_SCHEME).path(String.valueOf(R.mipmap.ic_launcher)).build()).setOldController(simpleDraweeView.getController()).setControllerListener(listener).build();
    simpleDraweeView.setController(controller);
    /**
     * 网络相关(retrofit+rxjava+rxlife+stetho)
     */
    OkHttpClient client = new OkHttpClient.Builder().connectTimeout(5000, TimeUnit.SECONDS).readTimeout(5000, TimeUnit.SECONDS).writeTimeout(5000, TimeUnit.SECONDS).addNetworkInterceptor(new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Headers headers = request.headers();
            Headers build = headers.newBuilder().add("version", "1.0").add("token", "xiaoxige").build();
            request = request.newBuilder().headers(build).build();
            Log.e("TAG", "url = " + request.url().uri().toString());
            String method = request.method();
            if (method.equals("GET") || method.equals("DELETE")) {
                HttpUrl url = request.url();
                HttpUrl httpUrl = url.newBuilder().addQueryParameter("xiaoxige", "one").addQueryParameter("zhuxiaoan", "two").build();
                request = request.newBuilder().url(httpUrl).build();
            } else {
                RequestBody body = request.body();
                if (body != null) {
                    Buffer buffer = new Buffer();
                    body.writeTo(buffer);
                    String readUtf8 = buffer.readUtf8();
                    // 可能需要对body进行加密
                    // TODO: 2017/11/3
                    RequestBody requestBody = RequestBody.create(body.contentType(), readUtf8);
                    request = request.newBuilder().method(method, requestBody).build();
                }
            }
            return chain.proceed(request);
        }
    }).addNetworkInterceptor(new StethoInterceptor()).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.baidu.com").addConverterFactory(ScalarsConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).client(client).build();
    final NetApi api = retrofit.create(NetApi.class);
    Flowable flowable = Flowable.create(new FlowableOnSubscribe<String>() {

        @Override
        public void subscribe(@NonNull FlowableEmitter<String> flowableEmitter) throws Exception {
            String response = MainActivity.execute(api.getBaiduWeb());
            if (TextUtils.isEmpty(response)) {
                flowableEmitter.onError(new Exception());
                return;
            }
            flowableEmitter.onNext(response);
            flowableEmitter.onComplete();
        }
    }, BackpressureStrategy.LATEST).compose(bindUntilEvent(ActivityEvent.DESTROY)).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());
    flowable.subscribe(new XXGSubscriber<String>() {

        @Override
        public void xxgNext(String o) {
            super.xxgNext(o);
            Log.e("TAG", "o = " + o);
        }

        @Override
        public void xxgError(Throwable t) {
            super.xxgError(t);
            Log.e("TAG", "t = " + t.getMessage());
        }

        @Override
        public void xxgComplete() {
            super.xxgComplete();
            Log.e("TAG", "Complete");
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GenericDraweeHierarchy(com.facebook.drawee.generic.GenericDraweeHierarchy) DraweeController(com.facebook.drawee.interfaces.DraweeController) Headers(okhttp3.Headers) ApplicationInfo(android.content.pm.ApplicationInfo) Uri(android.net.Uri) PackageManager(android.content.pm.PackageManager) BaseControllerListener(com.facebook.drawee.controller.BaseControllerListener) NonNull(io.reactivex.annotations.NonNull) FlowableEmitter(io.reactivex.FlowableEmitter) Interceptor(okhttp3.Interceptor) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) RoundingParams(com.facebook.drawee.generic.RoundingParams) RequestBody(okhttp3.RequestBody) Buffer(okio.Buffer) FlowableOnSubscribe(io.reactivex.FlowableOnSubscribe) Request(okhttp3.Request) Handler(android.os.Handler) ControllerListener(com.facebook.drawee.controller.ControllerListener) BaseControllerListener(com.facebook.drawee.controller.BaseControllerListener) IOException(java.io.IOException) SimpleDraweeView(com.facebook.drawee.view.SimpleDraweeView) View(android.view.View) HttpUrl(okhttp3.HttpUrl) IOException(java.io.IOException) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Animatable(android.graphics.drawable.Animatable) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) Nullable(javax.annotation.Nullable) Flowable(io.reactivex.Flowable)

Example 2 with FlowableEmitter

use of io.reactivex.FlowableEmitter in project Audient by komamj.

the class MainPresenter method blurBitmap.

@Override
public void blurBitmap(final Bitmap bitmap, final Context context, final int inSampleSize) {
    Disposable disposable = Flowable.create(new FlowableOnSubscribe<Drawable>() {

        @Override
        public void subscribe(FlowableEmitter<Drawable> emitter) throws Exception {
            Drawable drawable = ImageUtils.createBlurredImageFromBitmap(bitmap, context, inSampleSize);
            emitter.onNext(drawable);
            emitter.onComplete();
        }
    }, BackpressureStrategy.LATEST).subscribeOn(Schedulers.computation()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSubscriber<Drawable>() {

        @Override
        public void onNext(Drawable drawable) {
            if (mView != null) {
                mView.showBlurBackground(drawable);
            }
        }

        @Override
        public void onError(Throwable t) {
            LogUtils.e(TAG, "blurBitmap erorr : " + t.toString());
        }

        @Override
        public void onComplete() {
        }
    });
    mDisposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) FlowableOnSubscribe(io.reactivex.FlowableOnSubscribe) Drawable(android.graphics.drawable.Drawable) FlowableEmitter(io.reactivex.FlowableEmitter)

Example 3 with FlowableEmitter

use of io.reactivex.FlowableEmitter in project kripton by xcesco.

the class BindKripton180RawInsertSelectDataSource method execute.

public <T> Flowable<T> execute(final FlowableTransaction<T> transaction) {
    FlowableOnSubscribe<T> emitter = new FlowableOnSubscribe<T>() {

        @Override
        public void subscribe(FlowableEmitter<T> emitter) {
            boolean needToOpened = !BindKripton180RawInsertSelectDataSource.this.isOpenInWriteMode();
            boolean success = false;
            @SuppressWarnings("resource") SQLiteDatabase connection = needToOpened ? openWritableDatabase() : database();
            DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
            currentDaoFactory.onSessionOpened();
            try {
                connection.beginTransaction();
                if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
                    connection.setTransactionSuccessful();
                    success = true;
                }
                emitter.onComplete();
            } catch (Throwable e) {
                Logger.error(e.getMessage());
                e.printStackTrace();
                emitter.onError(e);
                currentDaoFactory.onSessionClear();
            } finally {
                try {
                    connection.endTransaction();
                } catch (Throwable e) {
                }
                if (needToOpened) {
                    close();
                }
                if (success) {
                    currentDaoFactory.onSessionClosed();
                } else {
                    currentDaoFactory.onSessionClear();
                }
            }
            return;
        }
    };
    Flowable<T> result = Flowable.create(emitter, BackpressureStrategy.BUFFER);
    if (globalSubscribeOn != null)
        result.subscribeOn(globalSubscribeOn);
    if (globalObserveOn != null)
        result.observeOn(globalObserveOn);
    return result;
}
Also used : FlowableOnSubscribe(io.reactivex.FlowableOnSubscribe) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) FlowableEmitter(io.reactivex.FlowableEmitter)

Example 4 with FlowableEmitter

use of io.reactivex.FlowableEmitter in project kripton by xcesco.

the class BindKripton180BeanInsertSelectDataSource method execute.

public <T> Flowable<T> execute(final FlowableTransaction<T> transaction) {
    FlowableOnSubscribe<T> emitter = new FlowableOnSubscribe<T>() {

        @Override
        public void subscribe(FlowableEmitter<T> emitter) {
            boolean needToOpened = !BindKripton180BeanInsertSelectDataSource.this.isOpenInWriteMode();
            boolean success = false;
            @SuppressWarnings("resource") SQLiteDatabase connection = needToOpened ? openWritableDatabase() : database();
            DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
            currentDaoFactory.onSessionOpened();
            try {
                connection.beginTransaction();
                if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
                    connection.setTransactionSuccessful();
                    success = true;
                }
                emitter.onComplete();
            } catch (Throwable e) {
                Logger.error(e.getMessage());
                e.printStackTrace();
                emitter.onError(e);
                currentDaoFactory.onSessionClear();
            } finally {
                try {
                    connection.endTransaction();
                } catch (Throwable e) {
                }
                if (needToOpened) {
                    close();
                }
                if (success) {
                    currentDaoFactory.onSessionClosed();
                } else {
                    currentDaoFactory.onSessionClear();
                }
            }
            return;
        }
    };
    Flowable<T> result = Flowable.create(emitter, BackpressureStrategy.BUFFER);
    if (globalSubscribeOn != null)
        result.subscribeOn(globalSubscribeOn);
    if (globalObserveOn != null)
        result.observeOn(globalObserveOn);
    return result;
}
Also used : FlowableOnSubscribe(io.reactivex.FlowableOnSubscribe) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) FlowableEmitter(io.reactivex.FlowableEmitter)

Example 5 with FlowableEmitter

use of io.reactivex.FlowableEmitter in project kripton by xcesco.

the class BindApp0DataSource method execute.

public <T> Flowable<T> execute(final FlowableTransaction<T> transaction) {
    FlowableOnSubscribe<T> emitter = new FlowableOnSubscribe<T>() {

        @Override
        public void subscribe(FlowableEmitter<T> emitter) {
            boolean needToOpened = !BindApp0DataSource.this.isOpenInWriteMode();
            boolean success = false;
            @SuppressWarnings("resource") SQLiteDatabase connection = needToOpened ? openWritableDatabase() : database();
            DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
            currentDaoFactory.onSessionOpened();
            try {
                connection.beginTransaction();
                if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
                    connection.setTransactionSuccessful();
                    success = true;
                }
                emitter.onComplete();
            } catch (Throwable e) {
                Logger.error(e.getMessage());
                e.printStackTrace();
                emitter.onError(e);
                currentDaoFactory.onSessionClear();
            } finally {
                try {
                    connection.endTransaction();
                } catch (Throwable e) {
                }
                if (needToOpened) {
                    close();
                }
                if (success) {
                    currentDaoFactory.onSessionClosed();
                } else {
                    currentDaoFactory.onSessionClear();
                }
            }
            return;
        }
    };
    Flowable<T> result = Flowable.create(emitter, BackpressureStrategy.BUFFER);
    if (globalSubscribeOn != null)
        result.subscribeOn(globalSubscribeOn);
    if (globalObserveOn != null)
        result.observeOn(globalObserveOn);
    return result;
}
Also used : FlowableOnSubscribe(io.reactivex.FlowableOnSubscribe) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) FlowableEmitter(io.reactivex.FlowableEmitter)

Aggregations

FlowableEmitter (io.reactivex.FlowableEmitter)6 FlowableOnSubscribe (io.reactivex.FlowableOnSubscribe)6 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageManager (android.content.pm.PackageManager)1 Animatable (android.graphics.drawable.Animatable)1 Drawable (android.graphics.drawable.Drawable)1 Uri (android.net.Uri)1 Handler (android.os.Handler)1 View (android.view.View)1 BaseControllerListener (com.facebook.drawee.controller.BaseControllerListener)1 ControllerListener (com.facebook.drawee.controller.ControllerListener)1 GenericDraweeHierarchy (com.facebook.drawee.generic.GenericDraweeHierarchy)1 RoundingParams (com.facebook.drawee.generic.RoundingParams)1 DraweeController (com.facebook.drawee.interfaces.DraweeController)1 SimpleDraweeView (com.facebook.drawee.view.SimpleDraweeView)1 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)1 Flowable (io.reactivex.Flowable)1 NonNull (io.reactivex.annotations.NonNull)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1