public class Constans {
    public static final String BASE_URL = "http://vscam.co/";
    public static final String WB_IMG_URL_PRE = "http://ww2.sinaimg.cn/bmiddle/%s";
    public static final String OG_IMG_URL_PRE = "http://vscam.co/img/m/%s.jpg";

    public static final String KEY_USER_INFO = "key_user_info";
    public static final String KEY_GRID = "key_grid";
    public static final String KEY_IMG_PATH = "key_img_path";

    public static final String TAG_REPORT = "tag_report";
} public class UserBean implements Serializable{

    /**
     * uid : 4
     * name : wsph123
     * avatar : 4
     * des : 我的个人简介
     * url : http://mouto.org
     */

    private int uid;
    private String name;
    private int avatar;
    private String error;
    private String des;
    private String url;

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAvatar() {
        return avatar;
    }

    public void setAvatar(int avatar) {
        this.avatar = avatar;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
} public class ApiIml {
    private static volatile ApiInterface sApiInterface;
    private static volatile OkHttpClient sOkHttpClient;

    public static ApiInterface getInstance(Context context){
        if(null == sApiInterface){
            synchronized(ApiIml.class){
                if(null == sApiInterface){
                    sApiInterface = new retrofit2.Retrofit.Builder().baseUrl(BASE_URL)
                            .client(getOkHttpClient(context))
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .addConverterFactory(FastJsonConverterFactory.create())
                            .build()
                            .create(ApiInterface.class);
                }
            }
        }
        return sApiInterface;
    }

    public static OkHttpClient getOkHttpClient(Context context){
        if(null == sOkHttpClient){
            synchronized(ApiIml.class){
                if(null == sOkHttpClient){
                    sOkHttpClient = new OkHttpBuilder()
                            .withCookie(new CookiesManager(context))
                            .withDefalutCache().getClient(context);
                }
            }
        }

        return sOkHttpClient;
    }
} public class ImgUploadUtils {

    public static void openGalleryForResult(final Activity context, final int requestCode){
        new RxPermissions(context).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .compose(RxUtils.<Boolean>applySchedulers())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean ok) throws Exception {
                        if(ok){
                            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            context.startActivityForResult(intent, requestCode);
                        }else {
                            showPermissionDialog(context);
                        }
                    }
                });
    }

    private static void showPermissionDialog(final Activity activity){
        SweetAlertDialog dialog = new SweetAlertDialog(activity, SweetAlertDialog.WARNING_TYPE);
        dialog.setTitleText("需要权限");
        dialog.setContentText("需要\"存储\"权限来打开您的照片");
        dialog.showCancelButton(true);
        dialog.setCancelText("拒绝");
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog.dismiss();
                activity.finish();
            }
        });
        dialog.setConfirmText("设置");
        dialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                ykooze.ayaseruri.codesslib.others.Utils.startAppSettings(activity);
            }
        });
        dialog.show();
    }
} public class ProcessDataUtils {
    public static ImgFlowBean addUserInfo(ImgFlowBean data){
        if(null == data){
            return null;
        }

        for(ImgFlowBean.GridsBean gridsBean : data.getGrids()){
            for (UserBean usersBean : data.getUsers()){
                if(gridsBean.getUid() == usersBean.getUid()){
                    gridsBean.setUserName(usersBean.getName());
                    break;
                }
            }
        }

        return data;
    }

    public static String getImgUrlS(ImgFlowBean.GridsBean gridsBean){
        String imgUrl;
        if(TextUtils.isEmpty(gridsBean.getWbpid())){
            imgUrl = String.format(Constans.OG_IMG_URL_PRE, gridsBean.getOrigin());
        }else {
            imgUrl = String.format(Constans.WB_IMG_URL_PRE, gridsBean.getWbpid());
        }
        return imgUrl;
    }

    public static Uri getAvatar(UserBean userBean){
        return 0 == userBean.getAvatar() ? Uri.parse("res:///" + R.mipmap.ic_default_avatar) : Uri.parse("http://vscam.co/avatar/b/"
                + userBean.getUid() + ".jpg");
    }
} public class StartUtils {
    public static void startMain(Context context){
        context.startActivity(new Intent(context, MainActivity_.class));
    }

    public static void startUpload(Context context){
        context.startActivity(new Intent(context, ImgUploadActivity_.class));
    }

    public static void startImgDetail(Context context, ImgFlowBean.GridsBean gridsBean, ActivityOptionsCompat options){
        Intent intent = new Intent(context, ImgDetailActivity_.class);
        intent.putExtra(Constans.KEY_GRID, (Parcelable) gridsBean);
        ActivityCompat.startActivity(context, intent, options.toBundle());
    }

    public static void startLogin(Context context, ActivityOptionsCompat options){
        ActivityCompat.startActivity(context, new Intent(context, LoginActivity_.class), options.toBundle());
    }

    public static void startRegister(Context context, ActivityOptionsCompat options){
        ActivityCompat.startActivity(context, new Intent(context, RegisterActivity_.class), options.toBundle());
    }

    public static void startImgViewer(Context context, String imgPath, ActivityOptionsCompat options){
        Intent intent = new Intent(context, ImgViewerActivity_.class);
        intent.putExtra(Constans.KEY_IMG_PATH, imgPath);
        ActivityCompat.startActivity(context, intent, options.toBundle());
    }

    public static void startAbout(Context context){
        context.startActivity(new Intent(context, AboutActivity_.class));
    }

    public static void startSettings(Context context, ActivityOptionsCompat options){
        Intent intent = new Intent(context, SettingsActivity_.class);
        ActivityCompat.startActivity(context, intent, options.toBundle());
    }

    public static void startUserDetail(Context context, ActivityOptionsCompat options){
        Intent intent = new Intent(context, UserDetailActivity_.class);
        ActivityCompat.startActivity(context, intent, options.toBundle());
    }
} public class UserInfoUtils {

    public static UserBean getUserInfo(Context context){
        return (UserBean) CacheUtils.get(context, Constans.KEY_USER_INFO, false);
    }

    public static void saveUserInfo(Context context, UserBean userBean){
        CacheUtils.putDisk(context, Constans.KEY_USER_INFO, userBean);
    }

    public static boolean isLogin(Context context){
        return getUserInfo(context) != null;
    }
} public class Utils {
    public static void share(Context context, String title, String content){
        Intent share_intent = new Intent();
        share_intent.setAction(Intent.ACTION_SEND);
        share_intent.setType("text/plain");
        share_intent.putExtra(Intent.EXTRA_SUBJECT, title);
        share_intent.putExtra(Intent.EXTRA_TEXT, content);

        share_intent = Intent.createChooser(share_intent, "分享到：");
        context.startActivity(share_intent);
    }

    public static void setDisplayHomeAsUp(final AppCompatActivity activity, final Toolbar toolbar){
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });
    }

    public static Snackbar getSnackBar(Activity activity, String message){
        View rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
        return Snackbar.make(rootView, message, duration);
    }
} public class CacheUtils {

    private static volatile HashMap<String, Object> sMemoryCache;

    public static void putMemory(String key, Object value){
        initMemoryCache();
        sMemoryCache.put(key, value);
    }

    public static <T extends Object & Serializable> void putDisk(Context context, String key, T t){
        initMemoryCache();
        sMemoryCache.put(key, t);
        SerializeUtils.serializationSync(context, key, t);
    }

    public static Object get(Context context, String key, boolean delete){
        initMemoryCache();
        if(sMemoryCache.containsKey(key)){
            return sMemoryCache.get(key);
        }else {
            Object object = null;
            try {
                 object = SerializeUtils.deserializationSync(context, key, delete);
                if(null != object && !delete){
                    sMemoryCache.put(key, object);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return object;
        }
    }

    public static void delete(Context context, String key){
        initMemoryCache();
        if(sMemoryCache.containsKey(key)){
            sMemoryCache.remove(key);
        }

        try {
            SerializeUtils.deserializationSync(context, key, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void deleteAll(){
        sMemoryCache = null;
        initMemoryCache();
    }

    private static void initMemoryCache(){
        if(null == sMemoryCache){
            synchronized (CacheUtils.class){
                if(null == sMemoryCache){
                    sMemoryCache = new HashMap<>();
                }
            }
        }
    }
} public class RxUtils {
    private volatile static Scheduler sChedulers;

    public static <T> ObservableTransformer<T, T> applySchedulers(){
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(io.reactivex.Observable<T> upstream) {
                return upstream.subscribeOn(getSchedulers())
                        .unsubscribeOn(getSchedulers())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    public static <T> FlowableTransformer<T, T> applyFlowableSchedulers(){
        return new FlowableTransformer<T, T>() {
            @Override
            public Publisher<T> apply(Flowable<T> upstream) {
                return upstream.subscribeOn(getSchedulers())
                        .unsubscribeOn(getSchedulers())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    public static <T extends Serializable> ObservableTransformer<T, T> applyCache(final Context context, final String tag){
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(io.reactivex.Observable<T> upstream) {
                return upstream.subscribeOn(getSchedulers())
                        .unsubscribeOn(getSchedulers())
                        .map(new Function<T, T>() {
                            @Override
                            public T apply(T t) throws Exception {
                                CacheUtils.putDisk(context, tag, t);
                                return t;
                            }
                        });
            }
        };
    }

    public static <T extends Serializable> ObservableTransformer<T, T> useCache(
            final Context context
            , final String tag
            , final boolean delete){
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(io.reactivex.Observable<T> upstream) {
                return io.reactivex.Observable.concat(
                        Observable.defer(new Callable<ObservableSource<? extends T>>() {
                            @Override
                            public ObservableSource<? extends T> call() throws Exception {
                                final Object object = CacheUtils.get(context, tag, delete);
                                return null == object ? Observable.<T>empty() : Observable.create(
                                        new ObservableOnSubscribe<T>() {
                                            @Override
                                            public void subscribe(ObservableEmitter<T> e) throws Exception {

                                                e.onNext((T) object);
                                                e.onComplete();
                                            }
                                        })
                                        .subscribeOn(getSchedulers())
                                        .unsubscribeOn(getSchedulers());
                            }
                        })
                        , upstream);
            }
        };
    }

    public static Scheduler getSchedulers() {
        if(null == sChedulers){
            synchronized (RxUtils.class){
                if(null == sChedulers){
                    int cupCores = Utils.getNumberOfCores();
                    int workNum = Math.min(cupCores * 2 + 1, 16);
                    sChedulers = Schedulers.from(new ThreadPoolExecutor(cupCores,
                            workNum,
                            1,
                            TimeUnit.SECONDS,
                            new LinkedBlockingQueue<Runnable>())
                    );
                }
            }
        }

        return sChedulers;
    }
}
import java.io.File;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.ViewById;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.x.vscam.R;
import com.x.vscam.global.Constans;
import com.x.vscam.global.bean.UserBean;
import com.x.vscam.global.net.ApiIml;
import com.x.vscam.global.utils.ImgUploadUtils;
import com.x.vscam.global.utils.ProcessDataUtils;
import com.x.vscam.global.utils.StartUtils;
import com.x.vscam.global.utils.UserInfoUtils;
import com.x.vscam.global.utils.Utils;
import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import ykooze.ayaseruri.codesslib.cache.CacheUtils;
import ykooze.ayaseruri.codesslib.io.FileUtils;
import ykooze.ayaseruri.codesslib.rx.RxActivity;
import ykooze.ayaseruri.codesslib.rx.RxUtils;
        super.onActivityResult(requestCode, resultCode, data);
        if(OPEN_GALLERY_REQUEST == requestCode){
            if(Activity.RESULT_OK == resultCode){
                String realImgPath = FileUtils.getUriPath(this, data.getData());
                if(TextUtils.isEmpty(realImgPath)){
                    finish();
                }else {
                    mAvatar.setImageURI(data.getData());
                    uploadAvatar(new File(realImgPath));
                }
            }else if(Activity.RESULT_CANCELED == resultCode){
                finish();
            }
        }
    }

    private void uploadAvatar(File imgFile){
        RequestBody imgRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), imgFile);
        ApiIml.getInstance(this).uploadAvatar(imgRequestBody).compose(RxUtils.<UploadAvatarResponseBean>applySchedulers())
                .compose(this.<UploadAvatarResponseBean>bindToLifecycle())
                .subscribe(new Observer<UploadAvatarResponseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mAvatarProBar.setVisibility(View.VISIBLE);
                        mEditAvatar.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void onNext(UploadAvatarResponseBean avatarResponseBean) {
                        Snackbar.make(mLinear, "头像修改成功", Snackbar.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mAvatarProBar.setVisibility(View.GONE);
                        mEditAvatar.setVisibility(View.VISIBLE);
                        e.printStackTrace();
                        Snackbar.make(mLinear, "头像上传失败", Snackbar.LENGTH_LONG).show();
                    }

                    @Override
                    public void onComplete() {
                        mAvatarProBar.setVisibility(View.GONE);
                        mEditAvatar.setVisibility(View.VISIBLE);
                    }
                });
    }

    @Click(R.id.edit_info)
    void onEditInfo(){
        String introduce = mIntroduce.getEditText().getText().toString();
        String blog = mBlog.getEditText().getText().toString();

        ApiIml.getInstance(this)
                .editInfo(introduce, blog)
                .compose(RxUtils.<SettingsBean>applySchedulers())
                .flatMap(new Function<SettingsBean, ObservableSource<SettingsBean>>() {
                    @Override
                    public ObservableSource<SettingsBean> apply(SettingsBean userBean) throws Exception {
                        if(TextUtils.isEmpty(userBean.getError())){
                            return Observable.just(userBean);
                        }else {
                            return Observable.error(new SettingsErrorException(userBean.getError()));
                        }
                    }
                })
                .subscribe(new Consumer<SettingsBean>() {
                    @Override
                    public void accept(SettingsBean settingsBean) throws Exception {
                        if(UserInfoUtils.isLogin(SettingsActivity.this)){
                            UserBean userBean = UserInfoUtils.getUserInfo(SettingsActivity.this);
                            userBean.setDes(settingsBean.getDes());
                            userBean.setUrl(settingsBean.getUrl());
                            UserInfoUtils.saveUserInfo(SettingsActivity.this, userBean);
                            Utils.getSnackBar(SettingsActivity.this, "信息修改成功").show();
                        }else {
                            Utils.getSnackBar(SettingsActivity.this, "信息修改出错").show();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if(throwable instanceof SettingsErrorException){
                            Utils.getSnackBar(SettingsActivity.this, throwable.getMessage()).show();
                        }
                    }
                });
    }

    @Click(R.id.del_avatar)
    void onDelAvatar(){
        ApiIml.getInstance(this).delAvatar().compose(RxUtils.<ResponseBody>applySchedulers())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Snackbar.make(mLinear, "已经删除头像", Snackbar.LENGTH_LONG).show();
                        UserBean userBean = UserInfoUtils.getUserInfo(SettingsActivity.this);
                        userBean.setAvatar(0);
                        UserInfoUtils.saveUserInfo(SettingsActivity.this, userBean);
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Click(R.id.logout)
    void onLogout(){
        CacheUtils.delete(this, Constans.KEY_USER_INFO);