From aea9985b68601fb78d20368d39f18c2aa2e81ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=B4=E9=BB=8E=E6=B2=A1=E6=9C=89=E6=91=A9=E5=A4=A9?= =?UTF-8?q?=E8=BD=AE?= <1056407060@qq.com> Date: Thu, 21 Sep 2017 10:52:03 +0800 Subject: [PATCH] Initial commit --- .gitignore | 9 + .idea/compiler.xml | 22 ++ .idea/copyright/profiles_settings.xml | 3 + .idea/gradle.xml | 19 + .idea/misc.xml | 49 +++ .idea/modules.xml | 10 + .idea/runConfigurations.xml | 12 + .idea/vcs.xml | 6 + app/.gitignore | 1 + app/build.gradle | 36 ++ app/proguard-rules.pro | 25 ++ app/src/main/AndroidManifest.xml | 37 ++ .../java/com/junlong0716/rxretrofit/Api.java | 46 +++ .../rxretrofit/DownloadService.java | 64 +++ .../rxretrofit/GlideImageLoader.java | 33 ++ .../junlong0716/rxretrofit/MainActivity.java | 245 ++++++++++++ .../com/junlong0716/rxretrofit/MyApp.java | 43 ++ .../junlong0716/rxretrofit/ResultModel.java | 60 +++ .../rxretrofit/event/DownloadEvent.java | 31 ++ .../junlong0716/rxretrofit/rxbus/BusData.java | 31 ++ .../junlong0716/rxretrofit/rxbus/RxBus.java | 368 ++++++++++++++++++ .../rxretrofit/rxbus/Subscribe.java | 20 + .../rxretrofit/rxbus/SubscriberMethod.java | 44 +++ .../rxretrofit/rxbus/ThreadMode.java | 23 ++ app/src/main/res/layout/activity_main.xml | 49 +++ app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 4208 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2555 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 6114 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10056 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10486 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 14696 bytes app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 11 + app/src/main/res/xml/file_paths.xml | 7 + build.gradle | 25 ++ gradle.properties | 17 + gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 53636 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 160 ++++++++ gradlew.bat | 90 +++++ retrofitutils/.gitignore | 1 + retrofitutils/build.gradle | 42 ++ retrofitutils/proguard-rules.pro | 25 ++ retrofitutils/src/main/AndroidManifest.xml | 13 + .../retrofitutils/BaseRetrofitClient.java | 74 ++++ .../retrofitutils/RetrofitUtils.java | 200 ++++++++++ .../retrofitutils/cache/CaheInterceptor.java | 55 +++ .../cache/NovateCookieManger.java | 57 +++ .../cache/PersistentCookieStore.java | 202 ++++++++++ .../cache/SerializableOkHttpCookies.java | 62 +++ .../download/DownLoadOnSubscribe.java | 104 +++++ .../download/DownLoadService.java | 19 + .../download/DownLoadSubscriber.java | 66 ++++ .../download/DownLoadTransformer.java | 40 ++ .../exception/ServerException.java | 23 ++ .../retrofitutils/log/LogInterceptor.java | 49 +++ .../retrofitutils/model/BaseModel.java | 16 + .../retrofitutils/model/ResultModel.java | 57 +++ .../upload/UploadOnSubscribe.java | 52 +++ .../upload/UploadRequestBody.java | 65 ++++ .../upload/UploadSubscriber.java | 76 ++++ .../retrofitutils/utils/FileUtils.java | 46 +++ .../retrofitutils/utils/NetworkUtils.java | 182 +++++++++ retrofitutils/src/main/res/values/strings.xml | 3 + settings.gradle | 1 + 70 files changed, 3141 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/junlong0716/rxretrofit/Api.java create mode 100644 app/src/main/java/com/junlong0716/rxretrofit/DownloadService.java create mode 100644 app/src/main/java/com/junlong0716/rxretrofit/GlideImageLoader.java create mode 100644 app/src/main/java/com/junlong0716/rxretrofit/MainActivity.java create mode 100644 app/src/main/java/com/junlong0716/rxretrofit/MyApp.java create mode 100644 app/src/main/java/com/junlong0716/rxretrofit/ResultModel.java create mode 100644 app/src/main/java/com/junlong0716/rxretrofit/event/DownloadEvent.java create mode 100755 app/src/main/java/com/junlong0716/rxretrofit/rxbus/BusData.java create mode 100755 app/src/main/java/com/junlong0716/rxretrofit/rxbus/RxBus.java create mode 100755 app/src/main/java/com/junlong0716/rxretrofit/rxbus/Subscribe.java create mode 100755 app/src/main/java/com/junlong0716/rxretrofit/rxbus/SubscriberMethod.java create mode 100755 app/src/main/java/com/junlong0716/rxretrofit/rxbus/ThreadMode.java create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/main/res/xml/file_paths.xml create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 retrofitutils/.gitignore create mode 100644 retrofitutils/build.gradle create mode 100644 retrofitutils/proguard-rules.pro create mode 100644 retrofitutils/src/main/AndroidManifest.xml create mode 100644 retrofitutils/src/main/java/com/junlong0716/retrofitutils/BaseRetrofitClient.java create mode 100644 retrofitutils/src/main/java/com/junlong0716/retrofitutils/RetrofitUtils.java create mode 100644 retrofitutils/src/main/java/com/junlong0716/retrofitutils/cache/CaheInterceptor.java create mode 100644 retrofitutils/src/main/java/com/junlong0716/retrofitutils/cache/NovateCookieManger.java create mode 100644 retrofitutils/src/main/java/com/junlong0716/retrofitutils/cache/PersistentCookieStore.java create mode 100644 retrofitutils/src/main/java/com/junlong0716/retrofitutils/cache/SerializableOkHttpCookies.java create mode 100755 retrofitutils/src/main/java/com/junlong0716/retrofitutils/download/DownLoadOnSubscribe.java create mode 100755 retrofitutils/src/main/java/com/junlong0716/retrofitutils/download/DownLoadService.java create mode 100755 retrofitutils/src/main/java/com/junlong0716/retrofitutils/download/DownLoadSubscriber.java create mode 100755 retrofitutils/src/main/java/com/junlong0716/retrofitutils/download/DownLoadTransformer.java create mode 100755 retrofitutils/src/main/java/com/junlong0716/retrofitutils/exception/ServerException.java create mode 100644 retrofitutils/src/main/java/com/junlong0716/retrofitutils/log/LogInterceptor.java create mode 100755 retrofitutils/src/main/java/com/junlong0716/retrofitutils/model/BaseModel.java create mode 100644 retrofitutils/src/main/java/com/junlong0716/retrofitutils/model/ResultModel.java create mode 100755 retrofitutils/src/main/java/com/junlong0716/retrofitutils/upload/UploadOnSubscribe.java create mode 100755 retrofitutils/src/main/java/com/junlong0716/retrofitutils/upload/UploadRequestBody.java create mode 100755 retrofitutils/src/main/java/com/junlong0716/retrofitutils/upload/UploadSubscriber.java create mode 100755 retrofitutils/src/main/java/com/junlong0716/retrofitutils/utils/FileUtils.java create mode 100644 retrofitutils/src/main/java/com/junlong0716/retrofitutils/utils/NetworkUtils.java create mode 100644 retrofitutils/src/main/res/values/strings.xml create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39fb081 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..b9e5db9 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..93b4dee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + $USER_HOME$/.subversion + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..75568db --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..f44b07c --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,36 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 26 + buildToolsVersion "26.0.1" + defaultConfig { + applicationId "com.junlong0716.rxretrofit" + minSdkVersion 19 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(include: ['*.jar'], dir: 'libs') + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:26.+' + compile 'com.android.support:design:26.+' + compile 'com.android.support.constraint:constraint-layout:1.0.2' + compile 'com.github.hss01248:NotifyUtil:1.0.1' + compile 'com.github.bumptech.glide:glide:4.1.1' + annotationProcessor 'com.github.bumptech.glide:compiler:4.1.1' + compile 'com.lzy.widget:imagepicker:0.6.1' + testCompile 'junit:junit:4.12' + compile project(':retrofitutils') +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..7e3f83c --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,25 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/mac/Library/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..22155ae --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/junlong0716/rxretrofit/Api.java b/app/src/main/java/com/junlong0716/rxretrofit/Api.java new file mode 100644 index 0000000..cb94e90 --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/Api.java @@ -0,0 +1,46 @@ +package com.junlong0716.rxretrofit; + +import java.util.ArrayList; + +import io.reactivex.Flowable; +import io.reactivex.Observable; +import okhttp3.MultipartBody; +import okhttp3.ResponseBody; +import retrofit2.http.Field; +import retrofit2.http.FormUrlEncoded; +import retrofit2.http.GET; +import retrofit2.http.Multipart; +import retrofit2.http.POST; +import retrofit2.http.Part; +import retrofit2.http.Query; +import retrofit2.http.Streaming; +import retrofit2.http.Url; + +/** + * Created by ${巴黎没有摩天轮Li} on 2017/9/19. + */ + +public interface Api { + String baseUrl = "http://order.htxcsoft.com/"; + + @GET("testForAz2.do") + Observable getFoodType(@Query("page") String page); + + @Multipart + @POST("testUploadPic.do") + Flowable> upload(@Part MultipartBody.Part file); + + + @Multipart + @POST("testUploadPic1.do") + Flowable> uploads(@Part ArrayList files); + + @FormUrlEncoded + @POST("testForAz1.do") + Observable getPostData(@Field("page") String page); + + @Streaming + @GET + Flowable startDownLoad(@Url String fileUrl); + +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/DownloadService.java b/app/src/main/java/com/junlong0716/rxretrofit/DownloadService.java new file mode 100644 index 0000000..8190285 --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/DownloadService.java @@ -0,0 +1,64 @@ +package com.junlong0716.rxretrofit; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; +import android.support.annotation.Nullable; +import android.widget.Toast; + + +import com.hss01248.notifyutil.NotifyUtil; +import com.junlong0716.retrofitutils.RetrofitUtils; +import com.junlong0716.retrofitutils.download.DownLoadSubscriber; +import com.junlong0716.rxretrofit.event.DownloadEvent; +import com.junlong0716.rxretrofit.rxbus.RxBus; + +import static android.support.v4.app.NotificationCompat.VISIBILITY_PUBLIC; + +/** + * Created by ${巴黎没有摩天轮Li} on 2017/9/20. + */ + +public class DownloadService extends Service { + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onCreate() { + super.onCreate(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + + RetrofitUtils.downLoadFile("qq.apk").safeSubscribe(new DownLoadSubscriber(this) { + @Override + protected void _onNext(String result) { + Toast.makeText(getApplicationContext(), "下载成功!", Toast.LENGTH_SHORT).show(); + RxBus.getDefault().post(new DownloadEvent("download_success", 100)); + } + + @Override + protected void _onProgress(Integer percent) { + NotifyUtil.buildProgress(102, R.mipmap.ic_launcher, "正在下载", percent, 100, "下载进度:%d%%") + .setOnGoing() + .setSmallIcon(R.mipmap.ic_launcher) + .setLockScreenVisiablity(VISIBILITY_PUBLIC) + .show(); + RxBus.getDefault().post(new DownloadEvent("download_running", percent)); + } + + @Override + protected void _onError(int errorCode, String msg) { + NotifyUtil.cancel(102); + RxBus.getDefault().post(new DownloadEvent("download_failed", 0)); + Toast.makeText(getApplicationContext(), "下载失败!", Toast.LENGTH_SHORT).show(); + } + }); + return super.onStartCommand(intent, flags, startId); + } +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/GlideImageLoader.java b/app/src/main/java/com/junlong0716/rxretrofit/GlideImageLoader.java new file mode 100644 index 0000000..165e951 --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/GlideImageLoader.java @@ -0,0 +1,33 @@ +package com.junlong0716.rxretrofit; + +import android.app.Activity; +import android.net.Uri; +import android.widget.ImageView; + +import com.bumptech.glide.Glide; +import com.lzy.imagepicker.loader.ImageLoader; + +import java.io.File; + +/** + * Created by ${巴黎没有摩天轮Li} on 2017/9/19. + */ + +public class GlideImageLoader implements ImageLoader { + @Override + public void displayImage(Activity activity, String path, ImageView imageView, int width, int height) { + Glide.with(activity)// + .load(Uri.fromFile(new File(path)))// + .into(imageView); + } + + @Override + public void displayImagePreview(Activity activity, String path, ImageView imageView, int width, int height) { + + } + + @Override + public void clearMemoryCache() { + + } +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/MainActivity.java b/app/src/main/java/com/junlong0716/rxretrofit/MainActivity.java new file mode 100644 index 0000000..c95ed3d --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/MainActivity.java @@ -0,0 +1,245 @@ +package com.junlong0716.rxretrofit; + +import android.content.Intent; +import android.net.Uri; +import android.os.Build; +import android.support.v4.content.FileProvider; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; + +import com.hss01248.notifyutil.NotifyUtil; +import com.junlong0716.retrofitutils.RetrofitUtils; +import com.junlong0716.retrofitutils.upload.UploadSubscriber; +import com.junlong0716.retrofitutils.utils.FileUtils; +import com.junlong0716.rxretrofit.event.DownloadEvent; +import com.junlong0716.rxretrofit.rxbus.RxBus; +import com.junlong0716.rxretrofit.rxbus.Subscribe; +import com.junlong0716.rxretrofit.rxbus.ThreadMode; +import com.lzy.imagepicker.ImagePicker; +import com.lzy.imagepicker.bean.ImageItem; +import com.lzy.imagepicker.ui.ImageGridActivity; +import com.lzy.imagepicker.view.CropImageView; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +import io.reactivex.Observer; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.annotations.NonNull; +import io.reactivex.disposables.Disposable; +import io.reactivex.schedulers.Schedulers; +import okhttp3.ResponseBody; + +public class MainActivity extends AppCompatActivity implements View.OnClickListener{ + + private int IMAGE_PICKER_SINGLE = 0; + private int IMAGE_PICKER_MUTI = 1; + private ProgressBar mBar; + private Intent mIntent; + private ImagePicker mImagePicker; + private TextView mTvProgress; + + //EventBus 3.0 回调 + @Subscribe(threadMode = ThreadMode.MAIN) + public void eventBus(DownloadEvent s) { + switch (s.getWhat()) { + case "download_running": + mBar.setProgress(s.getPercent()); + break; + case "download_success": + NotifyUtil.cancel(102); + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + File file = new File(FileUtils.getDefaultDownLoadPath(), "qq.apk"); + String path = file.getPath(); + Log.d("path", path); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 7.0+以上版本 + Uri apkUri = FileProvider.getUriForFile(this, "com.junlong0716.rxretrofit.fileprovider", file); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); + } else { + intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); + } + startActivity(intent); + break; + } + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + RxBus.getDefault().register(this); + initImagePicker(); + findViewById(R.id.bt_get_request).setOnClickListener(this); + findViewById(R.id.bt_upload_image).setOnClickListener(this); + findViewById(R.id.bt_upload_muti_image).setOnClickListener(this); + findViewById(R.id.bt_post_request).setOnClickListener(this); + findViewById(R.id.bt_download).setOnClickListener(this); + mBar = (ProgressBar) findViewById(R.id.pb); + mTvProgress = findViewById(R.id.tv_progress); + } + + private void initImagePicker() { + mImagePicker = ImagePicker.getInstance(); + mImagePicker.setImageLoader(new GlideImageLoader()); //设置图片加载器 + mImagePicker.setShowCamera(true); //显示拍照按钮 + mImagePicker.setCrop(true); //允许裁剪(单选才有效) + mImagePicker.setSaveRectangle(true); //是否按矩形区域保存 + mImagePicker.setStyle(CropImageView.Style.RECTANGLE); //裁剪框的形状 + mImagePicker.setFocusWidth(800); //裁剪框的宽度。单位像素(圆形自动取宽高最小值) + mImagePicker.setFocusHeight(800); //裁剪框的高度。单位像素(圆形自动取宽高最小值) + mImagePicker.setOutPutX(1000);//保存文件的宽度。单位像素 + mImagePicker.setOutPutY(1000);//保存文件的高度。单位像素 + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.bt_get_request: + RetrofitUtils.createService(Api.class).getFoodType("0").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Observer() { + @Override + public void onSubscribe(@NonNull Disposable d) { + + } + + @Override + public void onNext(@NonNull ResponseBody body) { + try { + Toast.makeText(getApplicationContext(), body.string(), Toast.LENGTH_SHORT).show(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + @Override + public void onError(@NonNull Throwable e) { + + } + + @Override + public void onComplete() { + + } + }); + break; + case R.id.bt_upload_image: + mImagePicker.setSelectLimit(1); //选中数量限制 + mIntent = new Intent(MainActivity.this, ImageGridActivity.class); + startActivityForResult(mIntent, IMAGE_PICKER_SINGLE); + break; + case R.id.bt_upload_muti_image: + mImagePicker.setSelectLimit(9); //选中数量限制 + mIntent = new Intent(MainActivity.this, ImageGridActivity.class); + startActivityForResult(mIntent, IMAGE_PICKER_MUTI); + break; + case R.id.bt_post_request: + RetrofitUtils.createService(Api.class).getPostData("0") + .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Observer() { + @Override + public void onSubscribe(@NonNull Disposable d) { + + } + + @Override + public void onNext(@NonNull ResponseBody body) { + try { + Toast.makeText(getApplicationContext(), body.string(), Toast.LENGTH_SHORT).show(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void onError(@NonNull Throwable e) { + + } + + @Override + public void onComplete() { + + } + }); + break; + case R.id.bt_download: + Intent startIntent = new Intent(this, DownloadService.class); + startService(startIntent); + break; + } + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == ImagePicker.RESULT_CODE_ITEMS) { + if (data != null && requestCode == IMAGE_PICKER_SINGLE) { + ArrayList images = (ArrayList) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS); + String path = images.get(0).path; + startUpload(path); + } else if (data != null && requestCode == IMAGE_PICKER_MUTI) { + ArrayList images = (ArrayList) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS); + startUploadMore(images); + } + } + } + + private void startUploadMore(ArrayList images) { + ArrayList filesList = new ArrayList<>(); + for (int i = 0; i < images.size(); i++) { + File file = new File(images.get(i).path); + filesList.add(file); + } + RetrofitUtils.uploadFiles(filesList, Api.class, "uploads").safeSubscribe(new UploadSubscriber(this) { + @Override + protected void _onNext(String result) { + Log.i("retrofit_onNext", "onNext=======>url:" + result); + Toast.makeText(getApplicationContext(), "上传成功", Toast.LENGTH_SHORT).show(); + + } + + @Override + protected void _onProgress(Integer percent) { + Log.i("retrofit_progress", "onProgress======>" + percent); + mBar.setProgress(percent); + } + + @Override + protected void _onError(int errorCode, String msg) { + Log.i("retrofit_onNext", "onNext=======>url:" + msg); + Toast.makeText(getApplicationContext(), "上传失败", Toast.LENGTH_SHORT).show(); + } + }); + } + + private void startUpload(String path) { + File file = new File(path); + RetrofitUtils.uploadFile(file, Api.class, "upload").safeSubscribe(new UploadSubscriber(this) { + @Override + protected void _onNext(String result) { + Log.i("retrofit_onNext", "onNext=======>url:" + result); + Toast.makeText(getApplicationContext(), "上传成功", Toast.LENGTH_SHORT).show(); + } + + @Override + protected void _onProgress(Integer percent) { + mBar.setProgress(percent); + } + + @Override + protected void _onError(int errorCode, String msg) { + Log.i("retrofit_onNext", "onNext=======>url:" + msg); + Toast.makeText(getApplicationContext(), "上传失败", Toast.LENGTH_SHORT).show(); + } + }); + } + +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/MyApp.java b/app/src/main/java/com/junlong0716/rxretrofit/MyApp.java new file mode 100644 index 0000000..c3c8d12 --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/MyApp.java @@ -0,0 +1,43 @@ +package com.junlong0716.rxretrofit; + +import android.app.Application; + +import com.hss01248.notifyutil.NotifyUtil; +import com.junlong0716.retrofitutils.BaseRetrofitClient; +import com.lzy.imagepicker.ImagePicker; +import com.lzy.imagepicker.view.CropImageView; + +/** + * Created by ${巴黎没有摩天轮Li} on 2017/9/21. + */ + +public class MyApp extends Application{ + private static MyApp myApplication = null; + + public static MyApp getApplication() { + return myApplication; + } + + @Override + public void onCreate() { + super.onCreate(); + myApplication = this; + initImagePicker(); + NotifyUtil.init(this); + BaseRetrofitClient.getInstance().setBaseUrl("http://order.htxcsoft.com/").init(this); + } + + private void initImagePicker() { + ImagePicker imagePicker = ImagePicker.getInstance(); + imagePicker.setImageLoader(new GlideImageLoader()); //设置图片加载器 + imagePicker.setShowCamera(true); //显示拍照按钮 + imagePicker.setCrop(true); //允许裁剪(单选才有效) + imagePicker.setSaveRectangle(true); //是否按矩形区域保存 + imagePicker.setSelectLimit(1); //选中数量限制 + imagePicker.setStyle(CropImageView.Style.RECTANGLE); //裁剪框的形状 + imagePicker.setFocusWidth(800); //裁剪框的宽度。单位像素(圆形自动取宽高最小值) + imagePicker.setFocusHeight(800); //裁剪框的高度。单位像素(圆形自动取宽高最小值) + imagePicker.setOutPutX(1000);//保存文件的宽度。单位像素 + imagePicker.setOutPutY(1000);//保存文件的高度。单位像素 + } +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/ResultModel.java b/app/src/main/java/com/junlong0716/rxretrofit/ResultModel.java new file mode 100644 index 0000000..1b14e73 --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/ResultModel.java @@ -0,0 +1,60 @@ +package com.junlong0716.rxretrofit; + + +import com.junlong0716.retrofitutils.model.BaseModel; + +/** + * Created by ${巴黎没有摩天轮Li} on 2017/9/19. + */ + +public class ResultModel implements BaseModel { + private int code; + + private T data; + + private String message; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public boolean isError() { + return code != 200; + } + + @Override + public int getErrorCode() { + return code; + } + + @Override + public String getMsg() { + return message; + } + + @Override + public T getResult() { + return data; + } +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/event/DownloadEvent.java b/app/src/main/java/com/junlong0716/rxretrofit/event/DownloadEvent.java new file mode 100644 index 0000000..28b7a6c --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/event/DownloadEvent.java @@ -0,0 +1,31 @@ +package com.junlong0716.rxretrofit.event; + +/** + * Created by ${巴黎没有摩天轮Li} on 2017/9/20. + */ + +public class DownloadEvent { + private String what; + private int percent; + + public DownloadEvent(String what, int percent) { + this.what = what; + this.percent = percent; + } + + public String getWhat() { + return what; + } + + public void setWhat(String what) { + this.what = what; + } + + public int getPercent() { + return percent; + } + + public void setPercent(int percent) { + this.percent = percent; + } +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/rxbus/BusData.java b/app/src/main/java/com/junlong0716/rxretrofit/rxbus/BusData.java new file mode 100755 index 0000000..f3e8007 --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/rxbus/BusData.java @@ -0,0 +1,31 @@ +package com.junlong0716.rxretrofit.rxbus; + +/** + * RxBus data + * Created by gorden on 2016/7/8. + */ +public class BusData { + String id; + String status; + public BusData() {} + public BusData(String id, String status) { + this.id = id; + this.status = status; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/rxbus/RxBus.java b/app/src/main/java/com/junlong0716/rxretrofit/rxbus/RxBus.java new file mode 100755 index 0000000..254dd3a --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/rxbus/RxBus.java @@ -0,0 +1,368 @@ +package com.junlong0716.rxretrofit.rxbus; + + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import io.reactivex.BackpressureStrategy; +import io.reactivex.Flowable; +import io.reactivex.Scheduler; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; +import io.reactivex.functions.Consumer; +import io.reactivex.functions.Function; +import io.reactivex.functions.Predicate; +import io.reactivex.schedulers.Schedulers; +import io.reactivex.subjects.PublishSubject; +import io.reactivex.subjects.Subject; + +/** + * RxBus + * Created by gorden on 2016/5/12. + * update 2017/3/1 + */ + +@SuppressWarnings("unused") +public class RxBus { + public static final String LOG_BUS = "RXBUS_LOG"; + private static volatile RxBus defaultInstance; + + private Map> subscriptionsByEventType = new HashMap<>(); + + private Map> eventTypesBySubscriber = new HashMap<>(); + + private Map> subscriberMethodByEventType = new HashMap<>(); + + private final Subject bus; + + private RxBus() { + this.bus = PublishSubject.create().toSerialized(); + } + + public static RxBus getDefault() { + RxBus rxBus = defaultInstance; + if (defaultInstance == null) { + synchronized (RxBus.class) { + rxBus = defaultInstance; + if (defaultInstance == null) { + rxBus = new RxBus(); + defaultInstance = rxBus; + } + } + } + return rxBus; + } + + /** + * 根据传递的 eventType 类型返回特定类型(eventType)的 被观察者 + * + * @param eventType 事件类型 + * @return return + */ + public Flowable toObservable(Class eventType) { + return bus.toFlowable(BackpressureStrategy.BUFFER).ofType(eventType); + } + + /** + * 根据传递的code和 eventType 类型返回特定类型(eventType)的 被观察者 + * + * @param code 事件code + * @param eventType 事件类型 + */ + private Flowable toObservable(final int code, final Class eventType) { + return bus.toFlowable(BackpressureStrategy.BUFFER).ofType(Message.class) + .filter(new Predicate() { + @Override + public boolean test(Message o) throws Exception { + return o.getCode() == code && eventType.isInstance(o.getObject()); + } + }).map(new Function() { + @Override + public Object apply(Message o) throws Exception { + return o.getObject(); + } + }).cast(eventType); + } + + /** + * 注册 + * + * @param subscriber 订阅者 + */ + public void register(Object subscriber) { + Class subClass = subscriber.getClass(); + Method[] methods = subClass.getDeclaredMethods(); + for (Method method : methods) { + if (method.isAnnotationPresent(Subscribe.class)) { + //获得参数类型 + Class[] parameterType = method.getParameterTypes(); + //参数不为空 且参数个数为1 + if (parameterType != null && parameterType.length == 1) { + + Class eventType = parameterType[0]; + + addEventTypeToMap(subscriber, eventType); + Subscribe sub = method.getAnnotation(Subscribe.class); + int code = sub.code(); + ThreadMode threadMode = sub.threadMode(); + + SubscriberMethod subscriberMethod = new SubscriberMethod(subscriber, method, eventType, code, threadMode); + addSubscriberToMap(eventType, subscriberMethod); + + addSubscriber(subscriberMethod); + } else if (parameterType == null || parameterType.length == 0) { + + Class eventType = BusData.class; + + addEventTypeToMap(subscriber, eventType); + Subscribe sub = method.getAnnotation(Subscribe.class); + int code = sub.code(); + ThreadMode threadMode = sub.threadMode(); + + SubscriberMethod subscriberMethod = new SubscriberMethod(subscriber, method, eventType, code, threadMode); + addSubscriberToMap(eventType, subscriberMethod); + + addSubscriber(subscriberMethod); + + } + } + } + } + + + /** + * 将event的类型以订阅中subscriber为key保存到map里 + * + * @param subscriber 订阅者 + * @param eventType event类型 + */ + private void addEventTypeToMap(Object subscriber, Class eventType) { + List eventTypes = eventTypesBySubscriber.get(subscriber); + if (eventTypes == null) { + eventTypes = new ArrayList<>(); + eventTypesBySubscriber.put(subscriber, eventTypes); + } + + if (!eventTypes.contains(eventType)) { + eventTypes.add(eventType); + } + } + + /** + * 将注解方法信息以event类型为key保存到map中 + * + * @param eventType event类型 + * @param subscriberMethod 注解方法信息 + */ + private void addSubscriberToMap(Class eventType, SubscriberMethod subscriberMethod) { + List subscriberMethods = subscriberMethodByEventType.get(eventType); + if (subscriberMethods == null) { + subscriberMethods = new ArrayList<>(); + subscriberMethodByEventType.put(eventType, subscriberMethods); + } + + if (!subscriberMethods.contains(subscriberMethod)) { + subscriberMethods.add(subscriberMethod); + } + } + + /** + * 将订阅事件以event类型为key保存到map,用于取消订阅时用 + * + * @param eventType event类型 + * @param disposable 订阅事件 + */ + private void addSubscriptionToMap(Class eventType, Disposable disposable) { + List disposables = subscriptionsByEventType.get(eventType); + if (disposables == null) { + disposables = new ArrayList<>(); + subscriptionsByEventType.put(eventType, disposables); + } + + if (!disposables.contains(disposable)) { + disposables.add(disposable); + } + } + + /** + * 用RxJava添加订阅者 + * + * @param subscriberMethod d + */ + @SuppressWarnings("unchecked") + private void addSubscriber(final SubscriberMethod subscriberMethod) { + Flowable flowable; + if (subscriberMethod.code == -1) { + flowable = toObservable(subscriberMethod.eventType); + } else { + flowable = toObservable(subscriberMethod.code, subscriberMethod.eventType); + } + Disposable subscription = postToObservable(flowable, subscriberMethod) + .subscribe(new Consumer() { + @Override + public void accept(Object o) throws Exception { + callEvent(subscriberMethod, o); + } + }); + + addSubscriptionToMap(subscriberMethod.subscriber.getClass(), subscription); + } + + /** + * 用于处理订阅事件在那个线程中执行 + * + * @param observable d + * @param subscriberMethod d + * @return Observable + */ + private Flowable postToObservable(Flowable observable, SubscriberMethod subscriberMethod) { + Scheduler scheduler; + switch (subscriberMethod.threadMode) { + case MAIN: + scheduler = AndroidSchedulers.mainThread(); + break; + + case NEW_THREAD: + scheduler = Schedulers.newThread(); + break; + + case CURRENT_THREAD: + scheduler = Schedulers.trampoline(); + break; + default: + throw new IllegalStateException("Unknown thread mode: " + subscriberMethod.threadMode); + } + return observable.observeOn(scheduler); + } + + /** + * 回调到订阅者的方法中 + * + * @param method code + * @param object obj + */ + private void callEvent(SubscriberMethod method, Object object) { + Class eventClass = object.getClass(); + List methods = subscriberMethodByEventType.get(eventClass); + if (methods != null && methods.size() > 0) { + for (SubscriberMethod subscriberMethod : methods) { + Subscribe sub = subscriberMethod.method.getAnnotation(Subscribe.class); + int c = sub.code(); + if (c == method.code && method.subscriber.equals(subscriberMethod.subscriber) && method.method.equals(subscriberMethod.method)) { + subscriberMethod.invoke(object); + } + + } + } + } + + + /** + * 是否注册 + * + * @param subscriber + * @return + */ + public synchronized boolean isRegistered(Object subscriber) { + return eventTypesBySubscriber.containsKey(subscriber); + } + + /** + * 取消注册 + * + * @param subscriber object + */ + public void unregister(Object subscriber) { + List subscribedTypes = eventTypesBySubscriber.get(subscriber); + if (subscribedTypes != null) { + for (Class eventType : subscribedTypes) { + unSubscribeByEventType(subscriber.getClass()); + unSubscribeMethodByEventType(subscriber, eventType); + } + eventTypesBySubscriber.remove(subscriber); + } + } + + /** + * subscriptions unsubscribe + * + * @param eventType eventType + */ + private void unSubscribeByEventType(Class eventType) { + List disposables = subscriptionsByEventType.get(eventType); + if (disposables != null) { + Iterator iterator = disposables.iterator(); + while (iterator.hasNext()) { + Disposable disposable = iterator.next(); + if (disposable != null && !disposable.isDisposed()) { + disposable.dispose(); + iterator.remove(); + } + } + } + } + + /** + * 移除subscriber对应的subscriberMethods + * + * @param subscriber subscriber + * @param eventType eventType + */ + private void unSubscribeMethodByEventType(Object subscriber, Class eventType) { + List subscriberMethods = subscriberMethodByEventType.get(eventType); + if (subscriberMethods != null) { + Iterator iterator = subscriberMethods.iterator(); + while (iterator.hasNext()) { + SubscriberMethod subscriberMethod = iterator.next(); + if (subscriberMethod.subscriber.equals(subscriber)) { + iterator.remove(); + } + } + } + } + + public void send(int code, Object o) { + bus.onNext(new Message(code, o)); + } + + public void post(Object o) { + bus.onNext(o); + } + + public void send(int code) { + bus.onNext(new Message(code, new BusData())); + } + + private class Message { + private int code; + private Object object; + + public Message() { + } + + private Message(int code, Object o) { + this.code = code; + this.object = o; + } + + private int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + private Object getObject() { + return object; + } + + public void setObject(Object object) { + this.object = object; + } + } +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/rxbus/Subscribe.java b/app/src/main/java/com/junlong0716/rxretrofit/rxbus/Subscribe.java new file mode 100755 index 0000000..27d1d31 --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/rxbus/Subscribe.java @@ -0,0 +1,20 @@ +package com.junlong0716.rxretrofit.rxbus; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Rxbus + * Created by gorden on 2016/7/23. + */ +@Documented +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Subscribe { + int code() default -1; + + ThreadMode threadMode() default ThreadMode.CURRENT_THREAD; +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/rxbus/SubscriberMethod.java b/app/src/main/java/com/junlong0716/rxretrofit/rxbus/SubscriberMethod.java new file mode 100755 index 0000000..cfb2d8d --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/rxbus/SubscriberMethod.java @@ -0,0 +1,44 @@ +package com.junlong0716.rxretrofit.rxbus; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * + * Created by gorden on 2016/7/23. + */ +public class SubscriberMethod { + public Method method; + public ThreadMode threadMode; + public Class eventType; + public Object subscriber; + public int code; + + public SubscriberMethod(Object subscriber, Method method, Class eventType, int code, ThreadMode threadMode) { + this.method = method; + this.threadMode = threadMode; + this.eventType = eventType; + this.subscriber = subscriber; + this.code = code; + } + + + /** + * 调用方法 + * @param o 参数 + */ + public void invoke(Object o){ + try { + Class[] parameterType = method.getParameterTypes(); + if(parameterType != null && parameterType.length == 1){ + method.invoke(subscriber, o); + }else if(parameterType == null || parameterType.length == 0){ + method.invoke(subscriber); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/main/java/com/junlong0716/rxretrofit/rxbus/ThreadMode.java b/app/src/main/java/com/junlong0716/rxretrofit/rxbus/ThreadMode.java new file mode 100755 index 0000000..9008edb --- /dev/null +++ b/app/src/main/java/com/junlong0716/rxretrofit/rxbus/ThreadMode.java @@ -0,0 +1,23 @@ +package com.junlong0716.rxretrofit.rxbus; + +/** + * + * Created by gorden on 2016/7/23. + */ +public enum ThreadMode { + /** + * current thread + */ + CURRENT_THREAD, + + /** + * android main thread + */ + MAIN, + + + /** + * new thread + */ + NEW_THREAD +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..3811d65 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,49 @@ + + + +