com.lzy.demo.okrx2.RxFileDownloadActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.lzy.demo.okrx2.RxFileDownloadActivity.java

Source

/*
 * Copyright 2016 jeasonlzy(?)
 *
 * 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 com.lzy.demo.okrx2;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.lzy.demo.R;
import com.lzy.demo.base.BaseRxDetailActivity;
import com.lzy.demo.ui.NumberProgressBar;
import com.lzy.demo.utils.Urls;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.convert.FileConvert;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.ObservableResponse;

import java.io.File;
import java.text.NumberFormat;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * ================================================
 *     jeasonlzy?Github?https://github.com/jeasonlzy
 *     1.0
 * 16/9/11
 * ??    
 * ?
 * ================================================
 */
public class RxFileDownloadActivity extends BaseRxDetailActivity {

    private static final int REQUEST_PERMISSION_STORAGE = 0x01;

    @Bind(R.id.et_url)
    EditText etUrl;
    @Bind(R.id.fileDownload1)
    Button btnFileDownload1;
    @Bind(R.id.fileDownload2)
    Button btnFileDownload2;
    @Bind(R.id.downloadSize)
    TextView tvDownloadSize;
    @Bind(R.id.tvProgress)
    TextView tvProgress;
    @Bind(R.id.netSpeed)
    TextView tvNetSpeed;
    @Bind(R.id.pbProgress)
    NumberProgressBar pbProgress;
    private NumberFormat numberFormat;

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_rx_file_download);
        ButterKnife.bind(this);
        setTitle("");

        numberFormat = NumberFormat.getPercentInstance();
        numberFormat.setMinimumFractionDigits(2);

        checkSDCardPermission();
    }

    /** SD??? */
    protected void checkSDCardPermission() {
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
                    REQUEST_PERMISSION_STORAGE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
            @android.support.annotation.NonNull String[] permissions,
            @android.support.annotation.NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //???
            } else {
                showToast("????");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Activity??
        dispose();
    }

    @OnClick(R.id.fileDownload1)
    public void fileDownload1(View view) {
        //okrx?,???
        String etString = etUrl.getText().toString();
        String url = TextUtils.isEmpty(etString) ? Urls.URL_DOWNLOAD : etString;
        OkGo.<File>get(url)//
                .headers("aaa", "111")//
                .params("bbb", "222")//
                .converter(new FileConvert())//
                .adapt(new ObservableResponse<File>()).subscribeOn(Schedulers.io())//
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        btnFileDownload1.setText(
                                "...\nRx????,???2");
                    }
                })//
                .observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new Observer<Response<File>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull Response<File> response) {
                        btnFileDownload1.setText("?");
                        handleResponse(response);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        btnFileDownload1.setText("");
                        showToast(e.getMessage());
                        handleError(null);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @OnClick(R.id.fileDownload2)
    public void fileDownload2(View view) {
        Observable.create(new ObservableOnSubscribe<Progress>() {
            @Override
            public void subscribe(@NonNull final ObservableEmitter<Progress> e) throws Exception {
                String etString = etUrl.getText().toString();
                String url = TextUtils.isEmpty(etString) ? Urls.URL_DOWNLOAD : etString;
                OkGo.<File>get(url)//
                        .headers("aaa", "111")//
                        .params("bbb", "222")//
                        .execute(new FileCallback() {
                            @Override
                            public void onSuccess(Response<File> response) {
                                e.onComplete();
                            }

                            @Override
                            public void onError(Response<File> response) {
                                e.onError(response.getException());
                            }

                            @Override
                            public void downloadProgress(Progress progress) {
                                e.onNext(progress);
                            }
                        });
            }
        })//
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        btnFileDownload2.setText("...");
                    }
                })//
                .observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new Observer<Progress>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull Progress progress) {
                        String downloadLength = Formatter.formatFileSize(getApplicationContext(),
                                progress.currentSize);
                        String totalLength = Formatter.formatFileSize(getApplicationContext(), progress.totalSize);
                        tvDownloadSize.setText(downloadLength + "/" + totalLength);
                        String speed = Formatter.formatFileSize(getApplicationContext(), progress.speed);
                        tvNetSpeed.setText(String.format("%s/s", speed));
                        tvProgress.setText(numberFormat.format(progress.fraction));
                        pbProgress.setMax(10000);
                        pbProgress.setProgress((int) (progress.fraction * 10000));
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        btnFileDownload2.setText("");
                        showToast(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        btnFileDownload2.setText("?");
                    }
                });
    }
}