Commit 9fdc4a8db325575a4293c4314f7459b2233d94da
1 parent
d8c0c1a275
Exists in
master
代码跑通
Showing
6 changed files
with
22 additions
and
52 deletions
Show diff stats
app/src/main/java/com/hjx/parent/fragment/MeFragment.java
1 | package com.hjx.parent.fragment; | 1 | package com.hjx.parent.fragment; |
2 | 2 | ||
3 | import android.app.AlertDialog; | ||
4 | import android.content.DialogInterface; | ||
5 | import android.content.Intent; | 3 | import android.content.Intent; |
6 | import android.text.TextUtils; | 4 | import android.text.TextUtils; |
7 | import android.view.View; | 5 | import android.view.View; |
8 | import android.widget.ImageView; | 6 | import android.widget.ImageView; |
9 | import android.widget.TextView; | 7 | import android.widget.TextView; |
10 | import android.widget.Toast; | ||
11 | 8 | ||
12 | import androidx.annotation.NonNull; | 9 | import androidx.annotation.NonNull; |
13 | import androidx.annotation.Nullable; | ||
14 | import androidx.constraintlayout.widget.Group; | 10 | import androidx.constraintlayout.widget.Group; |
15 | import androidx.recyclerview.widget.LinearLayoutManager; | 11 | import androidx.recyclerview.widget.LinearLayoutManager; |
16 | import androidx.recyclerview.widget.RecyclerView; | 12 | import androidx.recyclerview.widget.RecyclerView; |
17 | 13 | ||
18 | import com.bumptech.glide.Glide; | 14 | import com.bumptech.glide.Glide; |
19 | import com.bumptech.glide.load.resource.bitmap.RoundedCorners; | 15 | import com.bumptech.glide.load.resource.bitmap.RoundedCorners; |
20 | import com.bumptech.glide.request.RequestOptions; | 16 | import com.bumptech.glide.request.RequestOptions; |
21 | import com.chad.library.adapter.base.BaseQuickAdapter; | 17 | import com.chad.library.adapter.base.BaseQuickAdapter; |
22 | import com.chad.library.adapter.base.BaseViewHolder; | 18 | import com.chad.library.adapter.base.BaseViewHolder; |
23 | import com.developer.wang.utils.PermissionUtils; | ||
24 | import com.google.gson.Gson; | 19 | import com.google.gson.Gson; |
25 | import com.google.gson.JsonArray; | ||
26 | import com.hjq.permissions.OnPermissionCallback; | ||
27 | import com.hjq.permissions.Permission; | ||
28 | import com.hjq.permissions.XXPermissions; | ||
29 | import com.hjx.parent.AccountActivity; | 20 | import com.hjx.parent.AccountActivity; |
30 | import com.hjx.parent.AddStudentActivity; | 21 | import com.hjx.parent.AddStudentActivity; |
31 | import com.hjx.parent.EditStudentActivity; | 22 | import com.hjx.parent.EditStudentActivity; |
32 | import com.hjx.parent.LoginActivity; | ||
33 | import com.hjx.parent.MainActivity; | ||
34 | import com.hjx.parent.QRActivity; | ||
35 | import com.hjx.parent.R; | 23 | import com.hjx.parent.R; |
36 | import com.hjx.parent.UserActivity; | 24 | import com.hjx.parent.UserActivity; |
37 | import com.hjx.parent.YinsiActivity; | 25 | import com.hjx.parent.YinsiActivity; |
38 | import com.hjx.parent.bean.StudentBean; | 26 | import com.hjx.parent.bean.StudentBean; |
39 | import com.prws.common.base.BaseFragment; | 27 | import com.prws.common.base.BaseFragment; |
40 | import com.prws.common.net.NetWorks; | 28 | import com.prws.common.net.NetWorks; |
41 | import com.prws.common.utils.LogUtil; | 29 | import com.prws.common.utils.LogUtil; |
42 | import com.prws.common.utils.ScreenUtils; | 30 | import com.prws.common.utils.ScreenUtils; |
43 | import com.prws.common.utils.SharedPreferencesUtil; | 31 | import com.prws.common.utils.SharedPreferencesUtil; |
44 | 32 | ||
45 | import org.json.JSONArray; | 33 | import org.json.JSONArray; |
46 | import org.json.JSONObject; | 34 | import org.json.JSONObject; |
47 | 35 | ||
48 | import java.util.ArrayList; | 36 | import java.util.ArrayList; |
49 | import java.util.HashMap; | ||
50 | import java.util.List; | ||
51 | import java.util.Map; | ||
52 | 37 | ||
53 | import butterknife.BindView; | ||
54 | import butterknife.OnClick; | ||
55 | import io.reactivex.Observer; | 38 | import io.reactivex.Observer; |
56 | import io.reactivex.disposables.Disposable; | 39 | import io.reactivex.disposables.Disposable; |
57 | import okhttp3.ResponseBody; | 40 | import okhttp3.ResponseBody; |
58 | 41 | ||
59 | public class MeFragment extends BaseFragment { | 42 | public class MeFragment extends BaseFragment { |
60 | @Override | 43 | @Override |
61 | protected int getLayoutRes() { | 44 | protected int getLayoutRes() { |
62 | return R.layout.fragment_me; | 45 | return R.layout.fragment_me; |
63 | } | 46 | } |
64 | 47 | ||
65 | Group group_1; | 48 | Group group_1; |
66 | TextView tv_name; | 49 | TextView tv_name; |
67 | TextView tv_phone; | 50 | TextView tv_phone; |
68 | ImageView iv_toux; | 51 | ImageView iv_toux; |
69 | RecyclerView recycle; | 52 | RecyclerView recycle; |
70 | 53 | ||
71 | @Override | 54 | @Override |
72 | protected void initDatas() { | 55 | protected void initDatas() { |
73 | super.initDatas(); | 56 | super.initDatas(); |
74 | recycle = mRootView.findViewById(R.id.recycle); | 57 | recycle = mRootView.findViewById(R.id.recycle); |
75 | group_1 = mRootView.findViewById(R.id.group_1); | 58 | group_1 = mRootView.findViewById(R.id.group_1); |
76 | String role = (String) SharedPreferencesUtil.getData("role", ""); | 59 | String role = (String) SharedPreferencesUtil.getData("role", ""); |
77 | String userId = (String) SharedPreferencesUtil.getData("userId", ""); | 60 | String userId = (String) SharedPreferencesUtil.getData("userId", ""); |
78 | String token = (String) SharedPreferencesUtil.getData("token", ""); | 61 | String token = (String) SharedPreferencesUtil.getData("token", ""); |
79 | String phone = (String) SharedPreferencesUtil.getData("phone", ""); | 62 | String phone = (String) SharedPreferencesUtil.getData("phone", ""); |
80 | group_1.setVisibility(View.VISIBLE); | 63 | group_1.setVisibility(View.VISIBLE); |
81 | iv_toux = mRootView.findViewById(R.id.iv_toux); | 64 | iv_toux = mRootView.findViewById(R.id.iv_toux); |
82 | tv_name = mRootView.findViewById(R.id.tv_name); | 65 | tv_name = mRootView.findViewById(R.id.tv_name); |
83 | tv_phone = mRootView.findViewById(R.id.tv_phone); | 66 | tv_phone = mRootView.findViewById(R.id.tv_phone); |
84 | tv_phone.setVisibility(View.GONE); | 67 | tv_phone.setVisibility(View.GONE); |
85 | // tv_phone.setText(phone); | 68 | // tv_phone.setText(phone); |
86 | tv_name.setText(phone); | 69 | tv_name.setText(phone); |
87 | } | 70 | } |
88 | 71 | ||
89 | @Override | 72 | @Override |
90 | public void onResume() { | 73 | public void onResume() { |
91 | super.onResume(); | 74 | super.onResume(); |
92 | String photo = (String) SharedPreferencesUtil.getData("photo", ""); | 75 | String photo = (String) SharedPreferencesUtil.getData("photo", ""); |
93 | if (!TextUtils.isEmpty(photo)) { | 76 | if (!TextUtils.isEmpty(photo)) { |
94 | RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(getContext(), 45)); | 77 | RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(getContext(), 45)); |
95 | RequestOptions options = RequestOptions.bitmapTransform(roundedCorners); | 78 | RequestOptions options = RequestOptions.bitmapTransform(roundedCorners); |
96 | Glide.with(getContext()).load(photo).error(R.mipmap.ic_parent_avatar).apply(options).into(iv_toux); | 79 | Glide.with(getContext()).load(photo).error(R.mipmap.ic_parent_avatar).apply(options).into(iv_toux); |
97 | } | 80 | } |
98 | loadChildList(); | 81 | loadChildList(); |
99 | } | 82 | } |
100 | 83 | ||
101 | ArrayList<StudentBean> alist = new ArrayList<>(); | 84 | ArrayList<StudentBean> alist = new ArrayList<>(); |
102 | 85 | ||
103 | public void loadChildList() { | 86 | public void loadChildList() { |
104 | NetWorks.listChildren(new Observer<ResponseBody>() { | 87 | NetWorks.listChildren(new Observer<ResponseBody>() { |
105 | @Override | 88 | @Override |
106 | public void onSubscribe(Disposable d) { | 89 | public void onSubscribe(Disposable d) { |
107 | 90 | ||
108 | } | 91 | } |
109 | 92 | ||
110 | @Override | 93 | @Override |
111 | public void onNext(ResponseBody responseBody) { | 94 | public void onNext(ResponseBody responseBody) { |
112 | try { | 95 | try { |
113 | 96 | ||
114 | String str = responseBody.string().toString(); | 97 | String str = responseBody.string().toString(); |
115 | 98 | ||
116 | 99 | ||
117 | LogUtil.e("TAG", "----" + str); | 100 | LogUtil.e("TAG", "----" + str); |
118 | JSONObject jo = new JSONObject(str); | 101 | JSONObject jo = new JSONObject(str); |
119 | boolean isSucceed = jo.getBoolean("success"); | 102 | boolean isSucceed = jo.getBoolean("success"); |
120 | if (isSucceed) { | 103 | if (isSucceed) { |
121 | JSONArray jarr = jo.getJSONArray("data"); | 104 | JSONArray jarr = jo.getJSONArray("data"); |
122 | alist.clear(); | 105 | alist.clear(); |
123 | for (int i = 0; i < jarr.length(); i++) { | 106 | for (int i = 0; i < jarr.length(); i++) { |
124 | JSONObject jo2 = jarr.getJSONObject(i); | 107 | JSONObject jo2 = jarr.getJSONObject(i); |
125 | Gson gson = new Gson(); | 108 | Gson gson = new Gson(); |
126 | StudentBean sb = gson.fromJson(jo2.toString(), StudentBean.class); | 109 | StudentBean sb = gson.fromJson(jo2.toString(), StudentBean.class); |
127 | alist.add(sb); | 110 | alist.add(sb); |
128 | } | 111 | } |
129 | if (alist.size() > 0) { | 112 | if (alist.size() > 0) { |
130 | loadList(); | 113 | loadList(); |
131 | } | 114 | } |
132 | 115 | ||
133 | } else { | 116 | } else { |
134 | 117 | ||
135 | } | 118 | } |
136 | } catch (Exception e) { | 119 | } catch (Exception e) { |
137 | e.printStackTrace(); | 120 | e.printStackTrace(); |
138 | } | 121 | } |
139 | } | 122 | } |
140 | 123 | ||
141 | @Override | 124 | @Override |
142 | public void onError(Throwable e) { | 125 | public void onError(Throwable e) { |
143 | e.toString(); | 126 | e.toString(); |
144 | } | 127 | } |
145 | 128 | ||
146 | @Override | 129 | @Override |
147 | public void onComplete() { | 130 | public void onComplete() { |
148 | 131 | ||
149 | } | 132 | } |
150 | }); | 133 | }); |
151 | mRootView.findViewById(R.id.view_4).setOnClickListener(view -> startActivity(new Intent(getActivity(), YinsiActivity.class))); | 134 | mRootView.findViewById(R.id.view_4).setOnClickListener(view -> startActivity(new Intent(getActivity(), YinsiActivity.class))); |
152 | mRootView.findViewById(R.id.view_5).setOnClickListener(view -> startActivity(new Intent(getActivity(), UserActivity.class))); | 135 | mRootView.findViewById(R.id.view_5).setOnClickListener(view -> startActivity(new Intent(getActivity(), UserActivity.class))); |
153 | mRootView.findViewById(R.id.view_3).setOnClickListener(view -> startActivityForResult(new Intent(getActivity(), AccountActivity.class), 1003)); | 136 | mRootView.findViewById(R.id.view_3).setOnClickListener(view -> startActivityForResult(new Intent(getActivity(), AccountActivity.class), 1003)); |
154 | } | 137 | } |
155 | 138 | ||
156 | // @Override | 139 | // @Override |
157 | // public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | 140 | // public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
158 | // super.onActivityResult(requestCode, resultCode, data); | 141 | // super.onActivityResult(requestCode, resultCode, data); |
159 | // Intent intent = new Intent(getActivity(), LoginActivity.class); | 142 | // Intent intent = new Intent(getActivity(), LoginActivity.class); |
160 | // startActivity(intent); | 143 | // startActivity(intent); |
161 | // getActivity().finish(); | 144 | // getActivity().finish(); |
162 | // } | 145 | // } |
163 | 146 | ||
164 | public void loadList() { | 147 | public void loadList() { |
165 | StudentBean sb = new StudentBean(); | 148 | StudentBean sb = new StudentBean(); |
166 | sb.setNickName("添加学生"); | 149 | sb.setNickName("添加学生"); |
167 | alist.add(0, sb); | 150 | alist.add(0, sb); |
168 | BaseQuickAdapter adapter = new BaseQuickAdapter(R.layout.item_child, alist) { | 151 | BaseQuickAdapter adapter = new BaseQuickAdapter(R.layout.item_child, alist) { |
169 | @Override | 152 | @Override |
170 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { | 153 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { |
171 | 154 | ||
172 | } | 155 | } |
173 | 156 | ||
174 | @Override | 157 | @Override |
175 | protected void convert(@NonNull BaseViewHolder item, Object o) { | 158 | protected void convert(@NonNull BaseViewHolder item, Object o) { |
176 | StudentBean studentBean = alist.get(item.getAdapterPosition()); | 159 | StudentBean studentBean = alist.get(item.getAdapterPosition()); |
177 | item.setText(R.id.tv_name, studentBean.getNickName()); | 160 | item.setText(R.id.tv_name, studentBean.getNickName()); |
178 | if (TextUtils.isEmpty(studentBean.getStuId())) { | 161 | if (TextUtils.isEmpty(studentBean.getStuId())) { |
179 | item.setImageResource(R.id.iv_avatar, R.mipmap.tjxss); | 162 | item.setImageResource(R.id.iv_avatar, R.mipmap.tjxss); |
180 | item.setVisible(R.id.iv_vip, false); | 163 | item.setVisible(R.id.iv_vip, false); |
181 | item.getConvertView().setOnClickListener(new View.OnClickListener() { | 164 | item.getConvertView().setOnClickListener(new View.OnClickListener() { |
182 | @Override | 165 | @Override |
183 | public void onClick(View view) { | 166 | public void onClick(View view) { |
184 | startActivity(new Intent(getActivity(), AddStudentActivity.class)); | 167 | startActivity(new Intent(getActivity(), AddStudentActivity.class)); |
185 | } | 168 | } |
186 | }); | 169 | }); |
187 | } else { | 170 | } else { |
188 | if (TextUtils.isEmpty(studentBean.getPhoto())) { | 171 | if (TextUtils.isEmpty(studentBean.getPhoto())) { |
189 | item.setImageResource(R.id.iv_avatar, studentBean.getGender() == 0 ? R.mipmap.ic_avatar_male : R.mipmap.ic_avatar_female); | 172 | item.setImageResource(R.id.iv_avatar, studentBean.getGender() == 0 ? R.mipmap.ic_avatar_male : R.mipmap.ic_avatar_female); |
190 | } else { | 173 | } else { |
191 | RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(getContext(), 45)); | 174 | RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(getContext(), 45)); |
192 | RequestOptions options = RequestOptions.bitmapTransform(roundedCorners); | 175 | RequestOptions options = RequestOptions.bitmapTransform(roundedCorners); |
193 | Glide.with(getActivity()).load(studentBean.getPhoto()).apply(options).into((ImageView) item.getView(R.id.iv_avatar)); | 176 | Glide.with(getActivity()).load(studentBean.getPhoto()).apply(options).into((ImageView) item.getView(R.id.iv_avatar)); |
194 | } | 177 | } |
195 | item.setVisible(R.id.iv_vip, !"虚拟".equals(studentBean.getTeacherIdentity())); | 178 | item.setVisible(R.id.iv_vip, !"虚拟".equals(studentBean.getTeacherIdentity())); |
196 | item.getConvertView().setOnClickListener(new View.OnClickListener() { | 179 | item.getConvertView().setOnClickListener(new View.OnClickListener() { |
197 | @Override | 180 | @Override |
198 | public void onClick(View view) { | 181 | public void onClick(View view) { |
199 | startActivity(new Intent(getActivity(), EditStudentActivity.class).putExtra("student", new Gson().toJson(studentBean))); | 182 | startActivity(new Intent(getActivity(), EditStudentActivity.class).putExtra("student", new Gson().toJson(studentBean))); |
200 | } | 183 | } |
201 | }); | 184 | }); |
202 | } | 185 | } |
203 | } | 186 | } |
204 | }; | 187 | }; |
205 | recycle.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false)); | 188 | recycle.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false)); |
206 | recycle.setAdapter(adapter); | 189 | recycle.setAdapter(adapter); |
207 | } | 190 | } |
208 | 191 | ||
209 | } | 192 | } |
210 | 193 |
build.gradle
1 | buildscript { | 1 | buildscript { |
2 | 2 | ||
3 | 3 | ||
4 | ext { | 4 | ext { |
5 | 5 | ||
6 | 6 | ||
7 | androidId = [ | 7 | androidId = [ |
8 | compileSdkVersion: 32, | 8 | compileSdkVersion: 32, |
9 | minSdkVersion : 26, | 9 | minSdkVersion : 26, |
10 | targetSdkVersion : 32, | 10 | targetSdkVersion : 32, |
11 | versionCode : 1, | 11 | versionCode : 1, |
12 | versionName : "0.0.1" | 12 | versionName : "0.0.1" |
13 | 13 | ||
14 | 14 | ||
15 | ] | 15 | ] |
16 | 16 | ||
17 | 17 | ||
18 | url = [ | 18 | url = [ |
19 | server_url : "http://192.168.3.144:8088", //http 请求指令 | 19 | server_url : "http://192.168.3.144:8088", //http 请求指令 |
20 | server_url_online: "https://mgr.hjx.com", //http 请求指令 | 20 | server_url_online: "https://mgr.hjx.com", //http 请求指令 |
21 | ] | 21 | ] |
22 | 22 | ||
23 | 23 | ||
24 | dependencies = [ | 24 | dependencies = [ |
25 | appcompat : 'androidx.appcompat:appcompat:1.0.0', | 25 | appcompat : 'androidx.appcompat:appcompat:1.0.0', |
26 | constraint : 'androidx.constraintlayout:constraintlayout:1.1.3', | 26 | constraint : 'androidx.constraintlayout:constraintlayout:1.1.3', |
27 | cardview : 'androidx.cardview:cardview:1.0.0', | 27 | cardview : 'androidx.cardview:cardview:1.0.0', |
28 | recyclerview : 'androidx.recyclerview:recyclerview:1.0.0', | 28 | recyclerview : 'androidx.recyclerview:recyclerview:1.0.0', |
29 | material : 'com.google.android.material:material:1.0.0', | 29 | material : 'com.google.android.material:material:1.0.0', |
30 | // 依赖RxAndroid 2X 的依赖库 | 30 | // 依赖RxAndroid 2X 的依赖库 |
31 | // 增加RxJava 2X 的依赖库 | 31 | // 增加RxJava 2X 的依赖库 |
32 | rxandroid : 'io.reactivex.rxjava2:rxandroid:2.0.1', | 32 | rxandroid : 'io.reactivex.rxjava2:rxandroid:2.0.1', |
33 | rxjava : 'io.reactivex.rxjava2:rxjava:2.0.7', | 33 | rxjava : 'io.reactivex.rxjava2:rxjava:2.0.7', |
34 | 34 | ||
35 | // 以下需要使用Retrofit | 35 | // 以下需要使用Retrofit |
36 | //retrofit | 36 | //retrofit |
37 | retrofit : 'com.squareup.retrofit2:retrofit:2.1.0', | 37 | retrofit : 'com.squareup.retrofit2:retrofit:2.1.0', |
38 | //Gson converter | 38 | //Gson converter |
39 | converter : 'com.squareup.retrofit2:converter-gson:2.1.0', | 39 | converter : 'com.squareup.retrofit2:converter-gson:2.1.0', |
40 | //RxJava2 Adapter | 40 | //RxJava2 Adapter |
41 | retrofit2 : 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0', | 41 | retrofit2 : 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0', |
42 | //okhttp | 42 | //okhttp |
43 | okhttp : 'com.squareup.okhttp3:okhttp:3.8.1', | 43 | okhttp : 'com.squareup.okhttp3:okhttp:3.8.1', |
44 | logging : 'com.squareup.okhttp3:logging-interceptor:3.4.1', | 44 | logging : 'com.squareup.okhttp3:logging-interceptor:3.4.1', |
45 | utils : 'com.github.wang-developer:AndroidUtilsDemo:1.0.6', | 45 | // utils : 'com.github.wang-developer:AndroidUtilsDemo:1.0.6', |
46 | commons_lang : 'org.apache.commons:commons-lang3:3.7', | 46 | commons_lang : 'org.apache.commons:commons-lang3:3.7', |
47 | commons_compress: 'org.apache.commons:commons-compress:1.19', | 47 | commons_compress: 'org.apache.commons:commons-compress:1.19', |
48 | commons_c_lang : 'commons-lang:commons-lang:2.3', | 48 | commons_c_lang : 'commons-lang:commons-lang:2.3', |
49 | commons_codec : 'commons-codec:commons-codec:1.7', | 49 | commons_codec : 'commons-codec:commons-codec:1.7', |
50 | eventbus : 'org.greenrobot:eventbus:3.2.0', | 50 | eventbus : 'org.greenrobot:eventbus:3.2.0', |
51 | eventbus2 : 'org.simple:androideventbus:1.0.5.1', | 51 | eventbus2 : 'org.simple:androideventbus:1.0.5.1', |
52 | glide : 'com.github.bumptech.glide:glide:4.6.1', | 52 | glide : 'com.github.bumptech.glide:glide:4.6.1', |
53 | permissions : 'com.github.getActivity:XXPermissions:11.6', | 53 | permissions : 'com.github.getActivity:XXPermissions:18.5', |
54 | orcode : 'cn.yipianfengye.android:zxing-library:2.2' | 54 | orcode : 'cn.yipianfengye.android:zxing-library:2.2' |
55 | // push : 'com.umeng.sdk:push:4.1.0', | 55 | // push : 'com.umeng.sdk:push:4.1.0', |
56 | // litepal : 'org.litepal.android:java:3.0.0', | 56 | // litepal : 'org.litepal.android:java:3.0.0', |
57 | 57 | ||
58 | 58 | ||
59 | // AlivcFFmpeg : 'com.aliyun.video.android:AlivcFFmpeg:2.0.0', | 59 | // AlivcFFmpeg : 'com.aliyun.video.android:AlivcFFmpeg:2.0.0', |
60 | 60 | ||
61 | // | 61 | // |
62 | // basequickadapter : 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34', | 62 | // basequickadapter : 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34', |
63 | // SwipeDelMenuLayout: 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0', | 63 | // SwipeDelMenuLayout: 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0', |
64 | // flycoTabLayout : 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar' | 64 | // flycoTabLayout : 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar' |
65 | 65 | ||
66 | 66 | ||
67 | ] | 67 | ] |
68 | } | 68 | } |
69 | 69 | ||
70 | 70 | ||
71 | repositories { | 71 | repositories { |
72 | maven { url 'https://maven.aliyun.com/repository/gradle-plugin' } | 72 | maven { url 'https://maven.aliyun.com/repository/gradle-plugin' } |
73 | maven { url 'https://maven.aliyun.com/repository/public' } | ||
73 | maven { url 'https://maven.aliyun.com/repository/google' } | 74 | maven { url 'https://maven.aliyun.com/repository/google' } |
74 | maven { url 'https://maven.aliyun.com/repository/central' } | 75 | maven { url 'https://repo.huaweicloud.com/repository/maven-public/' } |
76 | //maven { url 'https://mirrors.cloud.tencent.com/nexus/repository/maven-public/' } | ||
77 | |||
75 | google() | 78 | google() |
76 | maven { url 'https://maven.aliyun.com/repository/jcenter' } | 79 | mavenCentral() |
80 | gradlePluginPortal() | ||
81 | maven { url "https://jitpack.io" } | ||
77 | } | 82 | } |
78 | dependencies { | 83 | dependencies { |
79 | classpath 'com.android.tools.build:gradle:4.2.2' | 84 | classpath 'com.android.tools.build:gradle:4.2.2' |
80 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4' | 85 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4' |
81 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' | 86 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' |
82 | classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3' | 87 | classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3' |
83 | classpath "com.alibaba:arouter-register:1.0.2" | 88 | classpath "com.alibaba:arouter-register:1.0.2" |
84 | // NOTE: Do not place your application dependencies here; they belong | 89 | // NOTE: Do not place your application dependencies here; they belong |
85 | // in the individual module build.gradle files | 90 | // in the individual module build.gradle files |
86 | } | 91 | } |
87 | 92 | ||
88 | 93 | ||
89 | } | 94 | } |
90 | 95 | ||
91 | 96 | ||
92 | allprojects { | 97 | allprojects { |
93 | repositories { | 98 | repositories { |
94 | maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' } | 99 | maven { url 'https://maven.aliyun.com/repository/public' } |
95 | maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } | 100 | maven { url 'https://maven.aliyun.com/repository/google' } |
96 | maven { url 'https://jitpack.io' } | 101 | maven { url 'https://repo.huaweicloud.com/repository/maven-public/' } |
102 | //maven { url 'https://mirrors.cloud.tencent.com/nexus/repository/maven-public/' } | ||
103 | |||
97 | google() | 104 | google() |
98 | maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' } | 105 | mavenCentral() |
106 | maven { url "https://jitpack.io" } | ||
99 | } | 107 | } |
100 | } | 108 | } |
101 | task clean(type: Delete) { | 109 | task clean(type: Delete) { |
102 | delete rootProject.buildDir | 110 | delete rootProject.buildDir |
103 | } | 111 | } |
104 | 112 |
libs/common/src/main/java/com/prws/common/base/BaseActivity.java
1 | package com.prws.common.base; | 1 | package com.prws.common.base; |
2 | 2 | ||
3 | 3 | ||
4 | import android.app.Activity; | 4 | import android.app.Activity; |
5 | import android.app.AlertDialog; | 5 | import android.app.AlertDialog; |
6 | import android.content.Context; | 6 | import android.content.Context; |
7 | import android.content.DialogInterface; | 7 | import android.content.DialogInterface; |
8 | import android.content.Intent; | 8 | import android.content.Intent; |
9 | import android.graphics.Color; | 9 | import android.graphics.Color; |
10 | import android.os.Bundle; | 10 | import android.os.Bundle; |
11 | import android.os.IBinder; | 11 | import android.os.IBinder; |
12 | import android.text.TextUtils; | 12 | import android.text.TextUtils; |
13 | import android.util.Log; | 13 | import android.util.Log; |
14 | import android.view.MotionEvent; | 14 | import android.view.MotionEvent; |
15 | import android.view.View; | 15 | import android.view.View; |
16 | import android.view.Window; | 16 | import android.view.Window; |
17 | import android.view.WindowManager; | 17 | import android.view.WindowManager; |
18 | import android.view.inputmethod.InputMethodManager; | 18 | import android.view.inputmethod.InputMethodManager; |
19 | import android.widget.EditText; | 19 | import android.widget.EditText; |
20 | import android.widget.Toast; | 20 | import android.widget.Toast; |
21 | 21 | ||
22 | import androidx.annotation.IdRes; | 22 | import androidx.annotation.IdRes; |
23 | import androidx.annotation.Nullable; | 23 | import androidx.annotation.Nullable; |
24 | import androidx.appcompat.app.AppCompatActivity; | 24 | import androidx.appcompat.app.AppCompatActivity; |
25 | import androidx.fragment.app.Fragment; | 25 | import androidx.fragment.app.Fragment; |
26 | import androidx.fragment.app.FragmentTransaction; | 26 | import androidx.fragment.app.FragmentTransaction; |
27 | import androidx.lifecycle.Lifecycle; | 27 | import androidx.lifecycle.Lifecycle; |
28 | 28 | ||
29 | import com.developer.wang.utils.PermissionUtils; | ||
30 | import com.prws.common.R; | 29 | import com.prws.common.R; |
31 | import com.prws.common.net.INetEvent; | 30 | import com.prws.common.net.INetEvent; |
32 | import com.prws.common.utils.LogUtil; | 31 | import com.prws.common.utils.LogUtil; |
33 | import com.prws.common.utils.ScreenUtils; | 32 | import com.prws.common.utils.ScreenUtils; |
34 | import com.prws.common.utils.SharedPreferencesUtil; | ||
35 | import com.prws.common.view.LoadingView; | 33 | import com.prws.common.view.LoadingView; |
36 | 34 | ||
37 | import org.simple.eventbus.EventBus; | 35 | import org.simple.eventbus.EventBus; |
38 | 36 | ||
39 | import java.util.ArrayList; | 37 | import java.util.ArrayList; |
40 | import java.util.List; | 38 | import java.util.List; |
41 | 39 | ||
42 | import butterknife.ButterKnife; | 40 | import butterknife.ButterKnife; |
43 | import io.reactivex.disposables.CompositeDisposable; | 41 | import io.reactivex.disposables.CompositeDisposable; |
44 | import io.reactivex.disposables.Disposable; | 42 | import io.reactivex.disposables.Disposable; |
45 | 43 | ||
46 | 44 | ||
47 | /** | 45 | /** |
48 | * @author 描述:BaseActivity | 46 | * @author 描述:BaseActivity |
49 | */ | 47 | */ |
50 | public abstract class BaseActivity<P extends BasePresenter, CONTRACT> extends AppCompatActivity implements INetEvent, BaseView { | 48 | public abstract class BaseActivity<P extends BasePresenter, CONTRACT> extends AppCompatActivity implements INetEvent, BaseView { |
51 | 49 | ||
52 | 50 | ||
53 | private LoadingView loading; | 51 | private LoadingView loading; |
54 | 52 | ||
55 | public static INetEvent mINetEvent; | 53 | public static INetEvent mINetEvent; |
56 | protected final String TAG = getClass().getSimpleName(); | 54 | protected final String TAG = getClass().getSimpleName(); |
57 | private CompositeDisposable mCompositeDisposable; | 55 | private CompositeDisposable mCompositeDisposable; |
58 | protected P p; | 56 | protected P p; |
59 | 57 | ||
60 | /** | 58 | /** |
61 | * 获取布局文件 | 59 | * 获取布局文件 |
62 | * | 60 | * |
63 | * @return | 61 | * @return |
64 | */ | 62 | */ |
65 | protected abstract int layoutResId(); | 63 | protected abstract int layoutResId(); |
66 | 64 | ||
67 | /** | 65 | /** |
68 | * 让P层做相应需求 | 66 | * 让P层做相应需求 |
69 | */ | 67 | */ |
70 | public abstract CONTRACT getContract(); | 68 | public abstract CONTRACT getContract(); |
71 | 69 | ||
72 | /** | 70 | /** |
73 | * 子类获取具体契约 | 71 | * 子类获取具体契约 |
74 | */ | 72 | */ |
75 | public abstract P getPresenter(); | 73 | public abstract P getPresenter(); |
76 | 74 | ||
77 | @Override | 75 | @Override |
78 | protected void onCreate(@Nullable Bundle savedInstanceState) { | 76 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
79 | super.onCreate(savedInstanceState); | 77 | super.onCreate(savedInstanceState); |
80 | setNetEvent(); | 78 | setNetEvent(); |
81 | initPresenter(); | 79 | initPresenter(); |
82 | showWhiteStatus(); | 80 | showWhiteStatus(); |
83 | setContentView(layoutResId()); | 81 | setContentView(layoutResId()); |
84 | ButterKnife.bind(this); | 82 | ButterKnife.bind(this); |
85 | registerEventBus(); | 83 | registerEventBus(); |
86 | initView(); | 84 | initView(); |
87 | initListener(); | 85 | initListener(); |
88 | initData(); | 86 | initData(); |
89 | 87 | ||
90 | } | 88 | } |
91 | 89 | ||
92 | 90 | ||
93 | public void showWhiteStatus() { | 91 | public void showWhiteStatus() { |
94 | Window window = getWindow(); | 92 | Window window = getWindow(); |
95 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | 93 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
96 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); | 94 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
97 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); | 95 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
98 | window.setStatusBarColor(Color.TRANSPARENT); | 96 | window.setStatusBarColor(Color.TRANSPARENT); |
99 | } | 97 | } |
100 | 98 | ||
101 | 99 | ||
102 | public void fullScreen(){ | 100 | public void fullScreen(){ |
103 | Window window = getWindow(); | 101 | Window window = getWindow(); |
104 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | 102 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
105 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | 103 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
106 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); | 104 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); |
107 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); | 105 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
108 | window.setStatusBarColor(Color.TRANSPARENT); | 106 | window.setStatusBarColor(Color.TRANSPARENT); |
109 | } | 107 | } |
110 | 108 | ||
111 | protected void blackBar() { | 109 | protected void blackBar() { |
112 | Window window = getWindow(); | 110 | Window window = getWindow(); |
113 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | 111 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
114 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | 112 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
115 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); | 113 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
116 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); | 114 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
117 | window.setStatusBarColor(Color.TRANSPARENT); | 115 | window.setStatusBarColor(Color.TRANSPARENT); |
118 | } | 116 | } |
119 | 117 | ||
120 | @Override | 118 | @Override |
121 | protected void onResume() { | 119 | protected void onResume() { |
122 | super.onResume(); | 120 | super.onResume(); |
123 | } | 121 | } |
124 | 122 | ||
125 | protected abstract void initView(); | 123 | protected abstract void initView(); |
126 | 124 | ||
127 | protected abstract void initListener(); | 125 | protected abstract void initListener(); |
128 | 126 | ||
129 | protected abstract void initData(); | 127 | protected abstract void initData(); |
130 | 128 | ||
131 | 129 | ||
132 | private void setNetEvent() { | 130 | private void setNetEvent() { |
133 | mINetEvent = this; | 131 | mINetEvent = this; |
134 | } | 132 | } |
135 | 133 | ||
136 | private void initPresenter() { | 134 | private void initPresenter() { |
137 | p = getPresenter(); | 135 | p = getPresenter(); |
138 | if (p != null) | 136 | if (p != null) |
139 | p.bindView(this); | 137 | p.bindView(this); |
140 | } | 138 | } |
141 | 139 | ||
142 | 140 | ||
143 | public void showDialog(String title, String message, String confirm, String cancel, DialogInterface.OnClickListener cancelListener, DialogInterface.OnClickListener listener) { | 141 | public void showDialog(String title, String message, String confirm, String cancel, DialogInterface.OnClickListener cancelListener, DialogInterface.OnClickListener listener) { |
144 | AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert); | 142 | AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert); |
145 | builder.setTitle(title); | 143 | builder.setTitle(title); |
146 | builder.setMessage(message); | 144 | builder.setMessage(message); |
147 | builder.setCancelable(false); | 145 | builder.setCancelable(false); |
148 | if (!TextUtils.isEmpty(cancel)) { | 146 | if (!TextUtils.isEmpty(cancel)) { |
149 | builder.setNegativeButton(cancel, cancelListener); | 147 | builder.setNegativeButton(cancel, cancelListener); |
150 | } | 148 | } |
151 | builder.setPositiveButton(confirm, listener); | 149 | builder.setPositiveButton(confirm, listener); |
152 | builder.show(); | 150 | builder.show(); |
153 | } | 151 | } |
154 | 152 | ||
155 | private void setWindow() { | 153 | private void setWindow() { |
156 | Window window = getWindow(); | 154 | Window window = getWindow(); |
157 | WindowManager.LayoutParams params = window.getAttributes(); | 155 | WindowManager.LayoutParams params = window.getAttributes(); |
158 | params.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN | 156 | params.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN |
159 | ; | 157 | ; |
160 | window.setAttributes(params); | 158 | window.setAttributes(params); |
161 | } | 159 | } |
162 | 160 | ||
163 | 161 | ||
164 | private void addNavigationBar() { | ||
165 | //判断是否有浮窗权限 | ||
166 | if (PermissionUtils.requestWindowManager(this)) { | ||
167 | //开启浮窗 | ||
168 | // openFloatWindow(); | ||
169 | } else { | ||
170 | showAlertDialogOfFloatWindow(); | ||
171 | } | ||
172 | } | ||
173 | |||
174 | // public void openFloatWindow() { | 162 | // public void openFloatWindow() { |
175 | // if (FloatWindow.get("navigation") == null) { | 163 | // if (FloatWindow.get("navigation") == null) { |
176 | // NavigationBarView navigationBarView = new NavigationBarView(this); | 164 | // NavigationBarView navigationBarView = new NavigationBarView(this); |
177 | // FloatWindow.with(CommonApplication.getAppContext()) | 165 | // FloatWindow.with(CommonApplication.getAppContext()) |
178 | // .setView(navigationBarView) | 166 | // .setView(navigationBarView) |
179 | // .setWidth(Screen.width, 1) | 167 | // .setWidth(Screen.width, 1) |
180 | // .setHeight(WindowManager.LayoutParams.WRAP_CONTENT) | 168 | // .setHeight(WindowManager.LayoutParams.WRAP_CONTENT) |
181 | // .setX(0) | 169 | // .setX(0) |
182 | // .setY(0) | 170 | // .setY(0) |
183 | // .setMoveType(MoveType.inactive) | 171 | // .setMoveType(MoveType.inactive) |
184 | // .setDesktopShow(false) | 172 | // .setDesktopShow(false) |
185 | // .setTag("navigation") | 173 | // .setTag("navigation") |
186 | // .build(); | 174 | // .build(); |
187 | // } | 175 | // } |
188 | // if (!FloatWindow.get("navigation").isShowing()) | 176 | // if (!FloatWindow.get("navigation").isShowing()) |
189 | // FloatWindow.get("navigation").show(); | 177 | // FloatWindow.get("navigation").show(); |
190 | // | 178 | // |
191 | // } | 179 | // } |
192 | 180 | ||
193 | 181 | ||
194 | public void showAlertDialogOfFloatWindow() { | 182 | public void showAlertDialogOfFloatWindow() { |
195 | //弹窗提示用户开启权限 | 183 | //弹窗提示用户开启权限 |
196 | // new AlertDialog(this).builder().setTitle(getString(R.string.need_permission)) | 184 | // new AlertDialog(this).builder().setTitle(getString(R.string.need_permission)) |
197 | // .setMsg(getString(R.string.need_permission_window)) | 185 | // .setMsg(getString(R.string.need_permission_window)) |
198 | // .setPositiveButton(getString(R.string.open), new View.OnClickListener() { | 186 | // .setPositiveButton(getString(R.string.open), new View.OnClickListener() { |
199 | // @Override | 187 | // @Override |
200 | // public void onClick(View v) { | 188 | // public void onClick(View v) { |
201 | // Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); | 189 | // Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); |
202 | // intent.setData(Uri.parse("package:" + getPackageName())); | 190 | // intent.setData(Uri.parse("package:" + getPackageName())); |
203 | // startActivity(intent); | 191 | // startActivity(intent); |
204 | // } | 192 | // } |
205 | // }).setNegativeButton(getString(R.string.cancel), new View.OnClickListener() { | 193 | // }).setNegativeButton(getString(R.string.cancel), new View.OnClickListener() { |
206 | // @Override | 194 | // @Override |
207 | // public void onClick(View v) { | 195 | // public void onClick(View v) { |
208 | // } | 196 | // } |
209 | // }).show(); | 197 | // }).show(); |
210 | LogUtil.toast("请开启浮窗权限"); | 198 | LogUtil.toast("请开启浮窗权限"); |
211 | } | 199 | } |
212 | 200 | ||
213 | 201 | ||
214 | public void showLoading(String title) { | 202 | public void showLoading(String title) { |
215 | synchronized (this) { | 203 | synchronized (this) { |
216 | if (loading != null || !isLiving(this)) return; | 204 | if (loading != null || !isLiving(this)) return; |
217 | runOnUiThread(() -> { | 205 | runOnUiThread(() -> { |
218 | loading = new LoadingView(this, R.style.CustomDialog, title); | 206 | loading = new LoadingView(this, R.style.CustomDialog, title); |
219 | loading.show(); | 207 | loading.show(); |
220 | ScreenUtils.hideBottomUIMenu(this); | 208 | ScreenUtils.hideBottomUIMenu(this); |
221 | ScreenUtils.hideDialogBottomUIMenu(loading); | 209 | ScreenUtils.hideDialogBottomUIMenu(loading); |
222 | }); | 210 | }); |
223 | } | 211 | } |
224 | } | 212 | } |
225 | 213 | ||
226 | 214 | ||
227 | public void setCanAble(boolean canAble) { | 215 | public void setCanAble(boolean canAble) { |
228 | if (loading != null) | 216 | if (loading != null) |
229 | loading.setCancelable(canAble); | 217 | loading.setCancelable(canAble); |
230 | 218 | ||
231 | } | 219 | } |
232 | 220 | ||
233 | public boolean isShowLoading() { | 221 | public boolean isShowLoading() { |
234 | if (loading == null) return false; | 222 | if (loading == null) return false; |
235 | return loading.isShowing(); | 223 | return loading.isShowing(); |
236 | } | 224 | } |
237 | 225 | ||
238 | public void dismissLoading() { | 226 | public void dismissLoading() { |
239 | runOnUiThread(() -> { | 227 | runOnUiThread(() -> { |
240 | if (loading == null || loading.isShowing() == false || !isLiving(this)) { | 228 | if (loading == null || loading.isShowing() == false || !isLiving(this)) { |
241 | Log.w(TAG, "loading == null" + | 229 | Log.w(TAG, "loading == null" + |
242 | " || loading.isShowing() == false >> return;"); | 230 | " || loading.isShowing() == false >> return;"); |
243 | return; | 231 | return; |
244 | } | 232 | } |
245 | loading.dismiss(); | 233 | loading.dismiss(); |
246 | loading = null; | 234 | loading = null; |
247 | 235 | ||
248 | }); | 236 | }); |
249 | } | 237 | } |
250 | 238 | ||
251 | 239 | ||
252 | private static boolean isLiving(Activity activity) { | 240 | private static boolean isLiving(Activity activity) { |
253 | 241 | ||
254 | if (activity == null) { | 242 | if (activity == null) { |
255 | Log.d("wisely", "activity == null"); | 243 | Log.d("wisely", "activity == null"); |
256 | return false; | 244 | return false; |
257 | } | 245 | } |
258 | 246 | ||
259 | if (activity.isFinishing()) { | 247 | if (activity.isFinishing()) { |
260 | Log.d("wisely", "activity is finishing"); | 248 | Log.d("wisely", "activity is finishing"); |
261 | return false; | 249 | return false; |
262 | } | 250 | } |
263 | 251 | ||
264 | return true; | 252 | return true; |
265 | } | 253 | } |
266 | 254 | ||
267 | 255 | ||
268 | /** | 256 | /** |
269 | * 是否需要弹窗提示更新 | 257 | * 是否需要弹窗提示更新 |
270 | */ | 258 | */ |
271 | protected void isNeedGetVersionUpdate() { | 259 | protected void isNeedGetVersionUpdate() { |
272 | // View view = findViewById(R.id.view_loading); | 260 | // View view = findViewById(R.id.view_loading); |
273 | //如果显示正在下载的页面,则不弹出更新弹窗 | 261 | //如果显示正在下载的页面,则不弹出更新弹窗 |
274 | // if (null == view || view.getVisibility() == View.VISIBLE) return; | 262 | // if (null == view || view.getVisibility() == View.VISIBLE) return; |
275 | // UpdateUtils.getVersion(this); | 263 | // UpdateUtils.getVersion(this); |
276 | } | 264 | } |
277 | 265 | ||
278 | 266 | ||
279 | /** | 267 | /** |
280 | * 全局检测网络广播的回调 处理网络变化 | 268 | * 全局检测网络广播的回调 处理网络变化 |
281 | * 注:在程序第一次启动的时候,没网并不会回调,需要自己在启动页面,或者主页自己再判断一次 | 269 | * 注:在程序第一次启动的时候,没网并不会回调,需要自己在启动页面,或者主页自己再判断一次 |
282 | * | 270 | * |
283 | * @param netWorkState 网络状态 -1:没网络 0:移动网络 1:WiFi网络 | 271 | * @param netWorkState 网络状态 -1:没网络 0:移动网络 1:WiFi网络 |
284 | */ | 272 | */ |
285 | public abstract void onNetChanged(int netWorkState); | 273 | public abstract void onNetChanged(int netWorkState); |
286 | 274 | ||
287 | @Override | 275 | @Override |
288 | public void onNetChange(int netWorkState) { | 276 | public void onNetChange(int netWorkState) { |
289 | onNetChanged(netWorkState); | 277 | onNetChanged(netWorkState); |
290 | } | 278 | } |
291 | 279 | ||
292 | @Override | 280 | @Override |
293 | protected void onDestroy() { | 281 | protected void onDestroy() { |
294 | super.onDestroy(); | 282 | super.onDestroy(); |
295 | unSubscribe(); | 283 | unSubscribe(); |
296 | cleanFragment(); | 284 | cleanFragment(); |
297 | unregisterEventBus(); | 285 | unregisterEventBus(); |
298 | if (p != null) { | 286 | if (p != null) { |
299 | p.onBindView(); | 287 | p.onBindView(); |
300 | p.cancelAllRequest(); | 288 | p.cancelAllRequest(); |
301 | } | 289 | } |
302 | } | 290 | } |
303 | 291 | ||
304 | @Override | 292 | @Override |
305 | protected void onPause() { | 293 | protected void onPause() { |
306 | super.onPause(); | 294 | super.onPause(); |
307 | 295 | ||
308 | } | 296 | } |
309 | 297 | ||
310 | private void registerEventBus() { | 298 | private void registerEventBus() { |
311 | EventBus.getDefault().register(this); | 299 | EventBus.getDefault().register(this); |
312 | } | 300 | } |
313 | 301 | ||
314 | private void unregisterEventBus() { | 302 | private void unregisterEventBus() { |
315 | EventBus.getDefault().unregister(this); | 303 | EventBus.getDefault().unregister(this); |
316 | } | 304 | } |
317 | 305 | ||
318 | public void showErrorToast() { | 306 | public void showErrorToast() { |
319 | // LogUtil.toast(MainApplication.getAppContext(), getString(R.string.service_error)); | 307 | // LogUtil.toast(MainApplication.getAppContext(), getString(R.string.service_error)); |
320 | } | 308 | } |
321 | 309 | ||
322 | public void showToast(String s) { | 310 | public void showToast(String s) { |
323 | Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show(); | 311 | Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show(); |
324 | } | 312 | } |
325 | 313 | ||
326 | private void unSubscribe() { | 314 | private void unSubscribe() { |
327 | if (mCompositeDisposable != null) { | 315 | if (mCompositeDisposable != null) { |
328 | mCompositeDisposable.clear(); | 316 | mCompositeDisposable.clear(); |
329 | } | 317 | } |
330 | } | 318 | } |
331 | 319 | ||
332 | protected void addSubscribe(Disposable disposable) { | 320 | protected void addSubscribe(Disposable disposable) { |
333 | if (mCompositeDisposable == null) { | 321 | if (mCompositeDisposable == null) { |
334 | mCompositeDisposable = new CompositeDisposable(); | 322 | mCompositeDisposable = new CompositeDisposable(); |
335 | } | 323 | } |
336 | mCompositeDisposable.add(disposable); | 324 | mCompositeDisposable.add(disposable); |
337 | } | 325 | } |
338 | 326 | ||
339 | protected void startActivity(Class clazz) { | 327 | protected void startActivity(Class clazz) { |
340 | startActivity(new Intent(this, clazz)); | 328 | startActivity(new Intent(this, clazz)); |
341 | } | 329 | } |
342 | 330 | ||
343 | /******************** 处理fragment *******************************/ | 331 | /******************** 处理fragment *******************************/ |
344 | List<Fragment> fragmentList = new ArrayList<>(); | 332 | List<Fragment> fragmentList = new ArrayList<>(); |
345 | 333 | ||
346 | protected void showFragment(Fragment fragment, @IdRes int id) { | 334 | protected void showFragment(Fragment fragment, @IdRes int id) { |
347 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); | 335 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); |
348 | 336 | ||
349 | if (!fragmentList.contains(fragment)) { | 337 | if (!fragmentList.contains(fragment)) { |
350 | fragmentList.add(fragment); | 338 | fragmentList.add(fragment); |
351 | fragmentTransaction.add(id, fragment, fragment.getClass().getSimpleName()).setMaxLifecycle(fragment, Lifecycle.State.RESUMED); | 339 | fragmentTransaction.add(id, fragment, fragment.getClass().getSimpleName()).setMaxLifecycle(fragment, Lifecycle.State.RESUMED); |
352 | } | 340 | } |
353 | 341 | ||
354 | fragmentTransaction.show(fragment).setMaxLifecycle(fragment, Lifecycle.State.RESUMED); | 342 | fragmentTransaction.show(fragment).setMaxLifecycle(fragment, Lifecycle.State.RESUMED); |
355 | 343 | ||
356 | for (Fragment fragment1 : fragmentList) { | 344 | for (Fragment fragment1 : fragmentList) { |
357 | if (fragment1 != fragment) { | 345 | if (fragment1 != fragment) { |
358 | fragmentTransaction.hide(fragment1).setMaxLifecycle(fragment1, Lifecycle.State.STARTED); | 346 | fragmentTransaction.hide(fragment1).setMaxLifecycle(fragment1, Lifecycle.State.STARTED); |
359 | } | 347 | } |
360 | } | 348 | } |
361 | 349 | ||
362 | fragmentTransaction.commitAllowingStateLoss(); | 350 | fragmentTransaction.commitAllowingStateLoss(); |
363 | 351 | ||
364 | } | 352 | } |
365 | 353 | ||
366 | protected void cleanFragment() { | 354 | protected void cleanFragment() { |
367 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); | 355 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); |
368 | if (fragmentList.size() > 0) { | 356 | if (fragmentList.size() > 0) { |
369 | for (Fragment fragment : fragmentList) { | 357 | for (Fragment fragment : fragmentList) { |
370 | fragmentTransaction.remove(fragment); | 358 | fragmentTransaction.remove(fragment); |
371 | } | 359 | } |
372 | try { | 360 | try { |
373 | fragmentTransaction.commitNowAllowingStateLoss(); | 361 | fragmentTransaction.commitNowAllowingStateLoss(); |
374 | } catch (Exception e) { | 362 | } catch (Exception e) { |
375 | e.printStackTrace(); | 363 | e.printStackTrace(); |
376 | } | 364 | } |
377 | } | 365 | } |
378 | fragmentList.clear(); | 366 | fragmentList.clear(); |
379 | } | 367 | } |
380 | 368 | ||
381 | //************************* 用于隐藏键盘 ***************************************// | 369 | //************************* 用于隐藏键盘 ***************************************// |
382 | @Override | 370 | @Override |
383 | public boolean dispatchTouchEvent(MotionEvent ev) { | 371 | public boolean dispatchTouchEvent(MotionEvent ev) { |
384 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { | 372 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
385 | 373 | ||
386 | View v = getCurrentFocus(); | 374 | View v = getCurrentFocus(); |
387 | if (isShouldHideInput(v, ev)) { | 375 | if (isShouldHideInput(v, ev)) { |
388 | hideSoftInput(v.getWindowToken()); | 376 | hideSoftInput(v.getWindowToken()); |
389 | } | 377 | } |
390 | } | 378 | } |
391 | return super.dispatchTouchEvent(ev); | 379 | return super.dispatchTouchEvent(ev); |
392 | } | 380 | } |
393 | 381 | ||
394 | private boolean isShouldHideInput(View v, MotionEvent event) { | 382 | private boolean isShouldHideInput(View v, MotionEvent event) { |
395 | if (v != null && (v instanceof EditText)) { | 383 | if (v != null && (v instanceof EditText)) { |
396 | int[] l = {0, 0}; | 384 | int[] l = {0, 0}; |
397 | v.getLocationInWindow(l); | 385 | v.getLocationInWindow(l); |
398 | int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left | 386 | int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left |
399 | + v.getWidth(); | 387 | + v.getWidth(); |
400 | if (event.getX() > left && event.getX() < right | 388 | if (event.getX() > left && event.getX() < right |
401 | && event.getY() > top && event.getY() < bottom) { | 389 | && event.getY() > top && event.getY() < bottom) { |
402 | // 点击EditText的事件,忽略它。 | 390 | // 点击EditText的事件,忽略它。 |
403 | return false; | 391 | return false; |
404 | } else { | 392 | } else { |
405 | return true; | 393 | return true; |
406 | } | 394 | } |
407 | } | 395 | } |
408 | 396 | ||
409 | return false; | 397 | return false; |
410 | } | 398 | } |
411 | 399 | ||
412 | /** | 400 | /** |
413 | * 多种隐藏软件盘方法的其中一种 | 401 | * 多种隐藏软件盘方法的其中一种 |
414 | * | 402 | * |
415 | * @param token | 403 | * @param token |
416 | */ | 404 | */ |
417 | private void hideSoftInput(IBinder token) { | 405 | private void hideSoftInput(IBinder token) { |
418 | LogUtil.i(TAG, "隐藏键盘"); | 406 | LogUtil.i(TAG, "隐藏键盘"); |
419 | if (token != null) { | 407 | if (token != null) { |
420 | InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); | 408 | InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); |
421 | im.hideSoftInputFromWindow(token, | 409 | im.hideSoftInputFromWindow(token, |
422 | InputMethodManager.HIDE_NOT_ALWAYS); | 410 | InputMethodManager.HIDE_NOT_ALWAYS); |
423 | } | 411 | } |
424 | } | 412 | } |
425 | 413 | ||
426 | //************************* 用于隐藏键盘 end***************************************// | 414 | //************************* 用于隐藏键盘 end***************************************// |
427 | 415 | ||
428 | 416 | ||
429 | } | 417 | } |
430 | 418 |
libs/common/src/main/java/com/prws/common/config/SettingConfig.java
1 | package com.prws.common.config; | 1 | package com.prws.common.config; |
2 | 2 | ||
3 | import android.content.Context; | 3 | import android.content.Context; |
4 | import android.content.SharedPreferences; | 4 | import android.content.SharedPreferences; |
5 | 5 | ||
6 | import com.developer.wang.utils.SharedPreferencesUtils; | 6 | import com.google.gson.Gson; |
7 | import com.developer.wang.utils.gson.MyGson; | ||
8 | import com.prws.common.CommonApplication; | 7 | import com.prws.common.CommonApplication; |
9 | 8 | ||
10 | 9 | ||
11 | public class SettingConfig { | 10 | public class SettingConfig { |
12 | private Boolean showDefaultImage = false; | 11 | private Boolean showDefaultImage = false; |
13 | private Boolean showLogo = false; | 12 | private Boolean showLogo = false; |
14 | 13 | ||
15 | private Boolean loadDisinfectModel = false; // 是否启用消杀 | 14 | private Boolean loadDisinfectModel = false; // 是否启用消杀 |
16 | private Boolean loadCleanModel = true; // 是否启用 | 15 | private Boolean loadCleanModel = true; // 是否启用 |
17 | private Boolean loadSidePushModel = false;//是否启用边推 | 16 | private Boolean loadSidePushModel = false;//是否启用边推 |
18 | 17 | ||
19 | private Integer cleanModelPressureLevel = 2; // 清洁模块的压力等级 | 18 | private Integer cleanModelPressureLevel = 2; // 清洁模块的压力等级 |
20 | 19 | ||
21 | private static volatile SettingConfig mInstance; | 20 | private static volatile SettingConfig mInstance; |
22 | 21 | ||
23 | public static SettingConfig getInstance() { | 22 | public static SettingConfig getInstance() { |
24 | if (mInstance == null) { | 23 | if (mInstance == null) { |
25 | synchronized (SettingConfig.class) { | 24 | synchronized (SettingConfig.class) { |
26 | if (mInstance == null) { | 25 | if (mInstance == null) { |
27 | String json = getSharedPreferences().getString("SettingConfig",""); | 26 | String json = getSharedPreferences().getString("SettingConfig",""); |
28 | if (json == null || json.length() == 0) { | 27 | if (json == null || json.length() == 0) { |
29 | mInstance = new SettingConfig(); | 28 | mInstance = new SettingConfig(); |
30 | } else { | 29 | } else { |
31 | mInstance = MyGson.buildGson().fromJson(json, SettingConfig.class); | 30 | mInstance = new Gson().fromJson(json, SettingConfig.class); |
32 | } | 31 | } |
33 | } | 32 | } |
34 | } | 33 | } |
35 | } | 34 | } |
36 | return mInstance; | 35 | return mInstance; |
37 | } | 36 | } |
38 | 37 | ||
39 | 38 | ||
40 | public static void cacheToDisk() { | 39 | public static void cacheToDisk() { |
41 | SharedPreferences sp = getSharedPreferences(); | 40 | SharedPreferences sp = getSharedPreferences(); |
42 | SharedPreferences.Editor editor = sp.edit(); | 41 | SharedPreferences.Editor editor = sp.edit(); |
43 | editor.putString("SettingConfig", MyGson.buildGson().toJson(getInstance())); | 42 | editor.putString("SettingConfig", new Gson().toJson(getInstance())); |
44 | editor.commit(); | 43 | editor.commit(); |
45 | } | 44 | } |
46 | public static SharedPreferences getSharedPreferences(){ | 45 | public static SharedPreferences getSharedPreferences(){ |
47 | return CommonApplication.getAppContext().getSharedPreferences("Delivery", Context.MODE_PRIVATE); | 46 | return CommonApplication.getAppContext().getSharedPreferences("Delivery", Context.MODE_PRIVATE); |
48 | } | 47 | } |
49 | 48 | ||
50 | public Boolean getShowDefaultImage() { | 49 | public Boolean getShowDefaultImage() { |
51 | return showDefaultImage; | 50 | return showDefaultImage; |
52 | } | 51 | } |
53 | 52 | ||
54 | public void setShowDefaultImage(Boolean showDefaultImage) { | 53 | public void setShowDefaultImage(Boolean showDefaultImage) { |
55 | this.showDefaultImage = showDefaultImage; | 54 | this.showDefaultImage = showDefaultImage; |
56 | } | 55 | } |
57 | 56 | ||
58 | public Boolean getShowLogo() { | 57 | public Boolean getShowLogo() { |
59 | return showLogo; | 58 | return showLogo; |
60 | } | 59 | } |
61 | 60 | ||
62 | public void setShowLogo(Boolean showLogo) { | 61 | public void setShowLogo(Boolean showLogo) { |
63 | this.showLogo = showLogo; | 62 | this.showLogo = showLogo; |
64 | } | 63 | } |
65 | 64 | ||
66 | public Boolean getLoadDisinfectModel() { | 65 | public Boolean getLoadDisinfectModel() { |
67 | return loadDisinfectModel; | 66 | return loadDisinfectModel; |
68 | } | 67 | } |
69 | 68 | ||
70 | public void setLoadDisinfectModel(Boolean loadDisinfectModel) { | 69 | public void setLoadDisinfectModel(Boolean loadDisinfectModel) { |
71 | this.loadDisinfectModel = loadDisinfectModel; | 70 | this.loadDisinfectModel = loadDisinfectModel; |
72 | } | 71 | } |
73 | 72 | ||
74 | public Boolean getLoadSidePushModel() { | 73 | public Boolean getLoadSidePushModel() { |
75 | return loadSidePushModel; | 74 | return loadSidePushModel; |
76 | } | 75 | } |
77 | 76 | ||
78 | public void setLoadSidePushModel(Boolean loadSidePushModel) { | 77 | public void setLoadSidePushModel(Boolean loadSidePushModel) { |
79 | this.loadSidePushModel = loadSidePushModel; | 78 | this.loadSidePushModel = loadSidePushModel; |
80 | } | 79 | } |
81 | 80 | ||
82 | public Boolean getLoadCleanModel() { | 81 | public Boolean getLoadCleanModel() { |
83 | return loadCleanModel; | 82 | return loadCleanModel; |
84 | } | 83 | } |
85 | 84 | ||
86 | public void setLoadCleanModel(Boolean loadCleanModel) { | 85 | public void setLoadCleanModel(Boolean loadCleanModel) { |
87 | this.loadCleanModel = loadCleanModel; | 86 | this.loadCleanModel = loadCleanModel; |
88 | } | 87 | } |
89 | 88 | ||
90 | public Integer getCleanModelPressureLevel() { | 89 | public Integer getCleanModelPressureLevel() { |
91 | return cleanModelPressureLevel; | 90 | return cleanModelPressureLevel; |
92 | } | 91 | } |
93 | 92 | ||
94 | public void setCleanModelPressureLevel(Integer cleanModelPressureLevel) { | 93 | public void setCleanModelPressureLevel(Integer cleanModelPressureLevel) { |
95 | this.cleanModelPressureLevel = cleanModelPressureLevel; | 94 | this.cleanModelPressureLevel = cleanModelPressureLevel; |
96 | } | 95 | } |
97 | } | 96 | } |
98 | 97 |
libs/common/src/main/java/com/prws/common/net/DownloadResponseBody.java
1 | package com.prws.common.net; | 1 | package com.prws.common.net; |
2 | 2 | ||
3 | 3 | import android.os.Handler; | |
4 | 4 | import android.os.Looper; | |
5 | import com.developer.wang.utils.MainHandler; | ||
6 | 5 | ||
7 | import okhttp3.MediaType; | 6 | import okhttp3.MediaType; |
8 | import okhttp3.ResponseBody; | 7 | import okhttp3.ResponseBody; |
9 | import okio.Buffer; | 8 | import okio.Buffer; |
10 | import okio.BufferedSource; | 9 | import okio.BufferedSource; |
11 | import okio.ForwardingSource; | 10 | import okio.ForwardingSource; |
12 | import okio.Okio; | 11 | import okio.Okio; |
13 | import okio.Source; | 12 | import okio.Source; |
14 | 13 | ||
15 | /** | 14 | /** |
16 | * @author | 15 | * @author |
17 | * | 16 | * |
18 | * 描述:DownloadResponseBody | 17 | * 描述:DownloadResponseBody |
19 | */ | 18 | */ |
20 | public class DownloadResponseBody extends ResponseBody { | 19 | public class DownloadResponseBody extends ResponseBody { |
21 | private ResponseBody responseBody; | 20 | private ResponseBody responseBody; |
22 | // BufferedSource 是okio库中的输入流,这里就当作inputStream来使用。 | 21 | // BufferedSource 是okio库中的输入流,这里就当作inputStream来使用。 |
23 | private BufferedSource bufferedSource; | 22 | private BufferedSource bufferedSource; |
24 | private DownloadListener mDownloadListener; | 23 | private DownloadListener mDownloadListener; |
25 | public DownloadResponseBody(ResponseBody responseBody, DownloadListener downloadListener) { | 24 | public DownloadResponseBody(ResponseBody responseBody, DownloadListener downloadListener) { |
26 | this.responseBody = responseBody; | 25 | this.responseBody = responseBody; |
27 | this.mDownloadListener = downloadListener; | 26 | this.mDownloadListener = downloadListener; |
28 | downloadListener.onStartDownload(responseBody.contentLength()); | 27 | downloadListener.onStartDownload(responseBody.contentLength()); |
29 | } | 28 | } |
30 | @Override | 29 | @Override |
31 | public MediaType contentType() { | 30 | public MediaType contentType() { |
32 | return responseBody.contentType(); | 31 | return responseBody.contentType(); |
33 | } | 32 | } |
34 | @Override | 33 | @Override |
35 | public long contentLength() { | 34 | public long contentLength() { |
36 | return responseBody.contentLength(); | 35 | return responseBody.contentLength(); |
37 | } | 36 | } |
38 | @Override | 37 | @Override |
39 | public BufferedSource source() { | 38 | public BufferedSource source() { |
40 | if (bufferedSource == null) { | 39 | if (bufferedSource == null) { |
41 | bufferedSource = Okio.buffer(source(responseBody.source())); | 40 | bufferedSource = Okio.buffer(source(responseBody.source())); |
42 | } | 41 | } |
43 | return bufferedSource; | 42 | return bufferedSource; |
44 | } | 43 | } |
45 | private Source source(Source source) { | 44 | private Source source(Source source) { |
46 | return new ForwardingSource(source) { | 45 | return new ForwardingSource(source) { |
47 | long totalBytesRead = 0L; | 46 | long totalBytesRead = 0L; |
48 | @Override | 47 | @Override |
49 | public long read(Buffer sink, long byteCount) { | 48 | public long read(Buffer sink, long byteCount) { |
50 | long bytesRead = 0; | 49 | long bytesRead = 0; |
51 | try { | 50 | try { |
52 | bytesRead = super.read(sink, byteCount); | 51 | bytesRead = super.read(sink, byteCount); |
53 | }catch (final Exception e){ | 52 | }catch (final Exception e){ |
54 | e.printStackTrace(); | 53 | e.printStackTrace(); |
55 | if (null != mDownloadListener) { | 54 | if (null != mDownloadListener) { |
56 | MainHandler.getInstance().post(new Runnable() { | 55 | new Handler(Looper.getMainLooper()).post(new Runnable() { |
57 | @Override | 56 | @Override |
58 | public void run() { | 57 | public void run() { |
59 | mDownloadListener.onError(e); | 58 | mDownloadListener.onError(e); |
60 | } | 59 | } |
61 | }); | 60 | }); |
62 | 61 | ||
63 | } | 62 | } |
64 | } | 63 | } |
65 | totalBytesRead += bytesRead != -1 ? bytesRead : 0; | 64 | totalBytesRead += bytesRead != -1 ? bytesRead : 0; |
66 | // LogUtil.e("download", "read: "+ (int) (totalBytesRead * 100 / responseBody.contentLength())); | 65 | // LogUtil.e("download", "read: "+ (int) (totalBytesRead * 100 / responseBody.contentLength())); |
67 | if (null != mDownloadListener) { | 66 | if (null != mDownloadListener) { |
68 | if (bytesRead != -1) { | 67 | if (bytesRead != -1) { |
69 | mDownloadListener.onProgress((int) (totalBytesRead * 100 / responseBody.contentLength())); | 68 | mDownloadListener.onProgress((int) (totalBytesRead * 100 / responseBody.contentLength())); |
70 | }else{ | 69 | }else{ |
71 | mDownloadListener.onDownloadComplete(); | 70 | mDownloadListener.onDownloadComplete(); |
72 | } | 71 | } |
73 | } | 72 | } |
74 | return bytesRead; | 73 | return bytesRead; |
75 | } | 74 | } |
76 | }; | 75 | }; |
77 | } | 76 | } |
78 | 77 | ||
79 | 78 | ||
80 | /** | 79 | /** |
81 | * length 文件总大小 | 80 | * length 文件总大小 |
82 | * | 81 | * |
83 | * progress 已下载进度 | 82 | * progress 已下载进度 |
84 | */ | 83 | */ |
85 | public interface DownloadListener { | 84 | public interface DownloadListener { |
86 | void onStartDownload(long length); | 85 | void onStartDownload(long length); |
87 | void onProgress(int progress); | 86 | void onProgress(int progress); |
88 | void onDownloadComplete(); | 87 | void onDownloadComplete(); |
89 | void onError(Exception e); | 88 | void onError(Exception e); |
90 | } | 89 | } |
91 | } | 90 | } |
92 | 91 |
libs/common/src/main/java/com/prws/common/utils/LogcatHelper.java
1 | package com.prws.common.utils; | 1 | package com.prws.common.utils; |
2 | 2 | ||
3 | import android.content.Context; | 3 | import android.content.Context; |
4 | import android.os.Environment; | 4 | import android.os.Environment; |
5 | import android.util.Log; | ||
6 | |||
7 | import com.developer.wang.utils.DateUtil; | ||
8 | 5 | ||
9 | import java.io.BufferedReader; | 6 | import java.io.BufferedReader; |
10 | import java.io.File; | 7 | import java.io.File; |
11 | import java.io.FileNotFoundException; | 8 | import java.io.FileNotFoundException; |
12 | import java.io.FileOutputStream; | 9 | import java.io.FileOutputStream; |
13 | import java.io.IOException; | 10 | import java.io.IOException; |
14 | import java.io.InputStreamReader; | 11 | import java.io.InputStreamReader; |
15 | import java.nio.file.Files; | ||
16 | import java.nio.file.Path; | ||
17 | import java.nio.file.attribute.BasicFileAttributes; | ||
18 | import java.text.ParseException; | 12 | import java.text.ParseException; |
19 | import java.text.SimpleDateFormat; | 13 | import java.text.SimpleDateFormat; |
20 | import java.time.Instant; | ||
21 | import java.util.Date; | 14 | import java.util.Date; |
22 | 15 | ||
23 | public class LogcatHelper { | 16 | public class LogcatHelper { |
24 | 17 | ||
25 | private static LogcatHelper INSTANCE = null; | 18 | private static LogcatHelper INSTANCE = null; |
26 | private static String PATH_LOGCAT; | 19 | private static String PATH_LOGCAT; |
27 | private LogDumper mLogDumper = null; | 20 | private LogDumper mLogDumper = null; |
28 | private int mPId; | 21 | private int mPId; |
29 | 22 | ||
30 | /** | 23 | /** |
31 | * 初始化目录 | 24 | * 初始化目录 |
32 | */ | 25 | */ |
33 | public void init(Context context) { | 26 | public void init(Context context) { |
34 | if (Environment.getExternalStorageState().equals( | 27 | if (Environment.getExternalStorageState().equals( |
35 | Environment.MEDIA_MOUNTED)) {// 优先保存到SD卡中 | 28 | Environment.MEDIA_MOUNTED)) {// 优先保存到SD卡中 |
36 | PATH_LOGCAT = Environment.getExternalStorageDirectory() | 29 | PATH_LOGCAT = Environment.getExternalStorageDirectory() |
37 | .getAbsolutePath() + File.separator + "aLog"; | 30 | .getAbsolutePath() + File.separator + "aLog"; |
38 | } else {// 如果SD卡不存在,就保存到本应用的目录下 | 31 | } else {// 如果SD卡不存在,就保存到本应用的目录下 |
39 | PATH_LOGCAT = context.getFilesDir().getAbsolutePath() | 32 | PATH_LOGCAT = context.getFilesDir().getAbsolutePath() |
40 | + File.separator + "aaLog"; | 33 | + File.separator + "aaLog"; |
41 | } | 34 | } |
42 | File file = new File(PATH_LOGCAT); | 35 | File file = new File(PATH_LOGCAT); |
43 | 36 | ||
44 | 37 | ||
45 | LogUtil.i("LogcatHelper", "存储位置:" + PATH_LOGCAT); | 38 | LogUtil.i("LogcatHelper", "存储位置:" + PATH_LOGCAT); |
46 | if (!file.exists()) { | 39 | if (!file.exists()) { |
47 | file.mkdirs(); | 40 | file.mkdirs(); |
48 | } | 41 | } |
49 | 42 | ||
50 | if (file.isDirectory()) { | 43 | if (file.isDirectory()) { |
51 | File[] files = file.listFiles(); | 44 | File[] files = file.listFiles(); |
52 | if (files.length > 3) { | 45 | if (files.length > 3) { |
53 | //记录最小的创建时间文件 | 46 | //记录最小的创建时间文件 |
54 | File minTimeFile = null; | 47 | File minTimeFile = null; |
55 | for (File file1 : files) { | 48 | for (File file1 : files) { |
56 | String fileName = file1.getName().substring(4, 14); | 49 | String fileName = file1.getName().substring(4, 14); |
57 | LogUtil.e("LogcatHelper", "fileName:" + fileName); | 50 | LogUtil.e("LogcatHelper", "fileName:" + fileName); |
58 | if (minTimeFile == null) { | 51 | if (minTimeFile == null) { |
59 | minTimeFile = file1; | 52 | minTimeFile = file1; |
60 | continue; | 53 | continue; |
61 | } else { | 54 | } else { |
62 | String minFileName = minTimeFile.getName().substring(4, 14); | 55 | String minFileName = minTimeFile.getName().substring(4, 14); |
63 | long minTime = getLongTime(minFileName); | 56 | long minTime = getLongTime(minFileName); |
64 | long curTime = getLongTime(fileName); | 57 | long curTime = getLongTime(fileName); |
65 | LogUtil.e("LogcatHelper", "minFileName:" + minFileName + "--minTime:" + minTime + "--curTime:" + curTime); | 58 | LogUtil.e("LogcatHelper", "minFileName:" + minFileName + "--minTime:" + minTime + "--curTime:" + curTime); |
66 | if (curTime < minTime) | 59 | if (curTime < minTime) |
67 | minTimeFile = file1; | 60 | minTimeFile = file1; |
68 | } | 61 | } |
69 | } | 62 | } |
70 | if (minTimeFile != null) { | 63 | if (minTimeFile != null) { |
71 | boolean isDelete = minTimeFile.delete(); | 64 | boolean isDelete = minTimeFile.delete(); |
72 | LogUtil.e("LogcatHelper", "isDelete:" + isDelete); | 65 | LogUtil.e("LogcatHelper", "isDelete:" + isDelete); |
73 | } | 66 | } |
74 | } | 67 | } |
75 | } | 68 | } |
76 | 69 | ||
77 | LogUtil.i("LogcatHelper", "文件是否存在:" + new File(PATH_LOGCAT).exists()); | 70 | LogUtil.i("LogcatHelper", "文件是否存在:" + new File(PATH_LOGCAT).exists()); |
78 | } | 71 | } |
79 | 72 | ||
80 | public static long getLongTime(String dateTime) { | 73 | public static long getLongTime(String dateTime) { |
81 | try { | 74 | try { |
82 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | 75 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
83 | Date startDate = sdf.parse(dateTime); | 76 | Date startDate = sdf.parse(dateTime); |
84 | return startDate.getTime(); | 77 | return startDate.getTime(); |
85 | } catch (ParseException e) { | 78 | } catch (ParseException e) { |
86 | e.printStackTrace(); | 79 | e.printStackTrace(); |
87 | } | 80 | } |
88 | return -1; | 81 | return -1; |
89 | } | 82 | } |
90 | 83 | ||
91 | 84 | ||
92 | public static LogcatHelper getInstance(Context context) { | 85 | public static LogcatHelper getInstance(Context context) { |
93 | if (INSTANCE == null) { | 86 | if (INSTANCE == null) { |
94 | INSTANCE = new LogcatHelper(context); | 87 | INSTANCE = new LogcatHelper(context); |
95 | } | 88 | } |
96 | return INSTANCE; | 89 | return INSTANCE; |
97 | } | 90 | } |
98 | 91 | ||
99 | private LogcatHelper(Context context) { | 92 | private LogcatHelper(Context context) { |
100 | init(context); | 93 | init(context); |
101 | mPId = android.os.Process.myPid(); | 94 | mPId = android.os.Process.myPid(); |
102 | } | 95 | } |
103 | 96 | ||
104 | public void start() { | 97 | public void start() { |
105 | if (mLogDumper == null) | 98 | if (mLogDumper == null) |
106 | mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT); | 99 | mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT); |
107 | mLogDumper.start(); | 100 | mLogDumper.start(); |
108 | } | 101 | } |
109 | 102 | ||
110 | public void stop() { | 103 | public void stop() { |
111 | if (mLogDumper != null) { | 104 | if (mLogDumper != null) { |
112 | mLogDumper.stopLogs(); | 105 | mLogDumper.stopLogs(); |
113 | mLogDumper = null; | 106 | mLogDumper = null; |
114 | } | 107 | } |
115 | } | 108 | } |
116 | 109 | ||
117 | private class LogDumper extends Thread { | 110 | private class LogDumper extends Thread { |
118 | private Process logcatProc; | 111 | private Process logcatProc; |
119 | private BufferedReader mReader = null; | 112 | private BufferedReader mReader = null; |
120 | private boolean mRunning = true; | 113 | private boolean mRunning = true; |
121 | String cmds = null; | 114 | String cmds = null; |
122 | private String mPID; | 115 | private String mPID; |
123 | private FileOutputStream out = null; | 116 | private FileOutputStream out = null; |
124 | 117 | ||
125 | public LogDumper(String pid, String dir) { | 118 | public LogDumper(String pid, String dir) { |
126 | mPID = pid; | 119 | mPID = pid; |
127 | try { | 120 | try { |
128 | out = new FileOutputStream(new File(dir, "log-" | 121 | out = new FileOutputStream(new File(dir, "log-" |
129 | + getFileName() + ".log"),true); | 122 | + getFileName() + ".log"),true); |
130 | } catch (FileNotFoundException e) { | 123 | } catch (FileNotFoundException e) { |
131 | // TODO Auto-generated catch block | 124 | // TODO Auto-generated catch block |
132 | e.printStackTrace(); | 125 | e.printStackTrace(); |
133 | } | 126 | } |
134 | /** | 127 | /** |
135 | * | 128 | * |
136 | * 日志等级:*:v , *:d , *:w , *:e , *:f , *:s | 129 | * 日志等级:*:v , *:d , *:w , *:e , *:f , *:s |
137 | * | 130 | * |
138 | * 显示当前mPID程序的 E和W等级的日志. | 131 | * 显示当前mPID程序的 E和W等级的日志. |
139 | * | 132 | * |
140 | * */ | 133 | * */ |
141 | // cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; | 134 | // cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; |
142 | // cmds = "logcat | grep \"(" + mPID + ")\"";//打印所有日志信息 | 135 | // cmds = "logcat | grep \"(" + mPID + ")\"";//打印所有日志信息 |
143 | // cmds = "logcat -s way";//打印标签过滤信息 | 136 | // cmds = "logcat -s way";//打印标签过滤信息 |
144 | // cmds = "logcat *:e *:i | grep \"(" + mPID + ")\""; | 137 | // cmds = "logcat *:e *:i | grep \"(" + mPID + ")\""; |
145 | cmds = "logcat | grep \"(" + mPID + ")\""; | 138 | cmds = "logcat | grep \"(" + mPID + ")\""; |
146 | 139 | ||
147 | } | 140 | } |
148 | 141 | ||
149 | public void stopLogs() { | 142 | public void stopLogs() { |
150 | mRunning = false; | 143 | mRunning = false; |
151 | } | 144 | } |
152 | 145 | ||
153 | @Override | 146 | @Override |
154 | public void run() { | 147 | public void run() { |
155 | try { | 148 | try { |
156 | logcatProc = Runtime.getRuntime().exec(cmds); | 149 | logcatProc = Runtime.getRuntime().exec(cmds); |
157 | mReader = new BufferedReader(new InputStreamReader( | 150 | mReader = new BufferedReader(new InputStreamReader( |
158 | logcatProc.getInputStream()), 1024); | 151 | logcatProc.getInputStream()), 1024); |
159 | String line = null; | 152 | String line = null; |
160 | while (mRunning && (line = mReader.readLine()) != null) { | 153 | while (mRunning && (line = mReader.readLine()) != null) { |
161 | if (!mRunning) { | 154 | if (!mRunning) { |
162 | break; | 155 | break; |
163 | } | 156 | } |
164 | if (line.length() == 0) { | 157 | if (line.length() == 0) { |
165 | continue; | 158 | continue; |
166 | } | 159 | } |
167 | if (out != null && line.contains(mPID)) { | 160 | if (out != null && line.contains(mPID)) { |
168 | out.write((line + "\n").getBytes()); | 161 | out.write((line + "\n").getBytes()); |
169 | } | 162 | } |
170 | } | 163 | } |
171 | 164 | ||
172 | } catch (IOException e) { | 165 | } catch (IOException e) { |
173 | e.printStackTrace(); | 166 | e.printStackTrace(); |
174 | } finally { | 167 | } finally { |
175 | if (logcatProc != null) { | 168 | if (logcatProc != null) { |
176 | logcatProc.destroy(); | 169 | logcatProc.destroy(); |
177 | logcatProc = null; | 170 | logcatProc = null; |
178 | } | 171 | } |
179 | if (mReader != null) { | 172 | if (mReader != null) { |
180 | try { | 173 | try { |
181 | mReader.close(); | 174 | mReader.close(); |
182 | mReader = null; | 175 | mReader = null; |
183 | } catch (IOException e) { | 176 | } catch (IOException e) { |
184 | e.printStackTrace(); | 177 | e.printStackTrace(); |
185 | } | 178 | } |
186 | } | 179 | } |
187 | if (out != null) { | 180 | if (out != null) { |
188 | try { | 181 | try { |
189 | out.close(); | 182 | out.close(); |
190 | } catch (IOException e) { | 183 | } catch (IOException e) { |
191 | e.printStackTrace(); | 184 | e.printStackTrace(); |
192 | } | 185 | } |
193 | out = null; | 186 | out = null; |
194 | } | 187 | } |
195 | } | 188 | } |
196 | } | 189 | } |
197 | } | 190 | } |
198 | 191 | ||
199 | public String getFileName() { | 192 | public String getFileName() { |
200 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); | 193 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); |
201 | String date = format.format(new Date(System.currentTimeMillis())); | 194 | String date = format.format(new Date(System.currentTimeMillis())); |
202 | return date; | 195 | return date; |
203 | } | 196 | } |
204 | } | 197 | } |