1
1
package com .idormy .sms .forwarder ;
2
2
3
3
import android .annotation .SuppressLint ;
4
- import android .app .ProgressDialog ;
5
4
import android .content .Context ;
6
5
import android .os .Bundle ;
7
- import android .os .Handler ;
8
- import android .os .Message ;
6
+ import android .os .Environment ;
9
7
import android .text .TextUtils ;
10
8
import android .util .Log ;
9
+ import android .view .View ;
11
10
import android .widget .Button ;
11
+ import android .widget .LinearLayout ;
12
+ import android .widget .RadioGroup ;
12
13
import android .widget .TextView ;
13
14
14
15
import androidx .annotation .NonNull ;
15
16
import androidx .appcompat .app .AppCompatActivity ;
16
17
17
18
import com .alibaba .fastjson .JSON ;
19
+ import com .hjq .permissions .OnPermissionCallback ;
20
+ import com .hjq .permissions .Permission ;
21
+ import com .hjq .permissions .XXPermissions ;
18
22
import com .hjq .toast .ToastUtils ;
19
23
import com .idormy .sms .forwarder .model .vo .CloneInfoVo ;
20
24
import com .idormy .sms .forwarder .receiver .BaseServlet ;
21
25
import com .idormy .sms .forwarder .receiver .RebootBroadcastReceiver ;
22
26
import com .idormy .sms .forwarder .sender .HttpServer ;
23
- import com .idormy .sms .forwarder .utils .BackupDbTask ;
27
+ import com .idormy .sms .forwarder .utils .CloneUtils ;
24
28
import com .idormy .sms .forwarder .utils .Define ;
25
- import com .idormy .sms .forwarder .utils .DownloadUtil ;
29
+ import com .idormy .sms .forwarder .utils .FileUtils ;
26
30
import com .idormy .sms .forwarder .utils .HttpUtil ;
27
31
import com .idormy .sms .forwarder .utils .NetUtil ;
28
32
import com .idormy .sms .forwarder .utils .SettingUtil ;
31
35
import java .io .File ;
32
36
import java .io .IOException ;
33
37
import java .util .HashMap ;
38
+ import java .util .List ;
34
39
import java .util .Map ;
35
40
import java .util .Objects ;
36
41
import java .util .concurrent .TimeUnit ;
@@ -47,37 +52,19 @@ public class CloneActivity extends AppCompatActivity {
47
52
private final String TAG = "CloneActivity" ;
48
53
private Context context ;
49
54
private String serverIp ;
50
- public static final String DATABASE_NAME = "sms_forwarder.db" ;
55
+ private String backupPath ;
56
+ private final String backupFile = "SmsForwarder.json" ;
51
57
private IPEditText textServerIp ;
52
58
private TextView sendTxt ;
53
59
private TextView receiveTxt ;
60
+ private TextView backupPathTxt ;
54
61
private Button sendBtn ;
55
- public static final int TOAST = 0x9731994 ;
56
- public static final int DOWNLOAD = 0x9731995 ;
57
-
58
- //消息处理者,创建一个Handler的子类对象,目的是重写Handler的处理消息的方法(handleMessage())
59
- @ SuppressWarnings ("deprecation" )
60
- @ SuppressLint ("HandlerLeak" )
61
- private final Handler handError = new Handler () {
62
- @ Override
63
- public void handleMessage (Message msg ) {
64
- if (msg .what == TOAST ) {
65
- ToastUtils .delayedShow (msg .getData ().getString ("DATA" ), 3000 );
66
- } else if (msg .what == DOWNLOAD ) {
67
- String savePath = context .getCacheDir ().getPath () + File .separator + BackupDbTask .BACKUP_FILE ;
68
- Log .d (TAG , savePath );
69
- downloadFile (msg .getData ().getString ("URL" ), context .getCacheDir ().getPath (), BackupDbTask .BACKUP_FILE , msg .getData ().getString ("INFO" ));
70
- }
71
- }
72
- };
73
62
74
63
@ Override
75
64
public void onCreate (Bundle savedInstanceState ) {
76
65
Log .d (TAG , "onCreate" );
77
66
super .onCreate (savedInstanceState );
78
67
79
- context = CloneActivity .this ;
80
-
81
68
setContentView (R .layout .activity_clone );
82
69
Log .d (TAG , "onCreate: " + RebootBroadcastReceiver .class .getName ());
83
70
@@ -92,6 +79,41 @@ protected void onStart() {
92
79
super .onStart ();
93
80
Log .d (TAG , "onStart" );
94
81
82
+ backupPathTxt = findViewById (R .id .backupPathTxt );
83
+ // 申请储存权限
84
+ XXPermissions .with (this ).permission (Permission .Group .STORAGE ).request (new OnPermissionCallback () {
85
+ @ Override
86
+ public void onGranted (List <String > permissions , boolean all ) {
87
+ backupPath = Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DOWNLOADS ).getPath ();
88
+ backupPathTxt .setText (backupPath + File .separator + backupFile );
89
+ }
90
+
91
+ @ Override
92
+ public void onDenied (List <String > permissions , boolean never ) {
93
+ if (never ) {
94
+ ToastUtils .show (R .string .toast_denied_never );
95
+ // 如果是被永久拒绝就跳转到应用权限系统设置页面
96
+ XXPermissions .startPermissionActivity (CloneActivity .this , permissions );
97
+ } else {
98
+ ToastUtils .show (R .string .toast_denied );
99
+ }
100
+ backupPathTxt .setText ("未授权储存权限,该功能无法使用!" );
101
+ }
102
+ });
103
+
104
+ LinearLayout layoutNetwork = findViewById (R .id .layoutNetwork );
105
+ LinearLayout layoutOffline = findViewById (R .id .layoutOffline );
106
+ final RadioGroup radioGroupTypeCheck = findViewById (R .id .radioGroupTypeCheck );
107
+ radioGroupTypeCheck .setOnCheckedChangeListener ((group , checkedId ) -> {
108
+ if (checkedId == R .id .btnTypeOffline ) {
109
+ layoutNetwork .setVisibility (View .GONE );
110
+ layoutOffline .setVisibility (View .VISIBLE );
111
+ } else {
112
+ layoutNetwork .setVisibility (View .VISIBLE );
113
+ layoutOffline .setVisibility (View .GONE );
114
+ }
115
+ });
116
+
95
117
sendBtn = findViewById (R .id .sendBtn );
96
118
sendTxt = findViewById (R .id .sendTxt );
97
119
TextView ipText = findViewById (R .id .ipText );
@@ -110,18 +132,14 @@ protected void onStart() {
110
132
sendBtn .setText (R .string .send );
111
133
sendTxt .setText (R .string .server_has_stopped );
112
134
}
113
- //noinspection CommentedOutCode
135
+
136
+ //发送
114
137
sendBtn .setOnClickListener (v -> {
115
138
if (!HttpServer .asRunning () && NetUtil .NETWORK_WIFI != NetUtil .getNetWorkStatus ()) {
116
- Toast ( handError , TAG , getString (R .string .no_wifi_network ));
139
+ ToastUtils . show ( getString (R .string .no_wifi_network ));
117
140
return ;
118
141
}
119
142
120
- //备份文件
121
- //BackupDbTask task = new BackupDbTask(this);
122
- //String backup_version = task.doInBackground(BackupDbTask.COMMAND_BACKUP);
123
- //Log.d(TAG, "backup_version = " + backup_version);
124
-
125
143
SettingUtil .switchEnableHttpServer (!SettingUtil .getSwitchEnableHttpServer ());
126
144
if (!HttpServer .update ()) {
127
145
SettingUtil .switchEnableHttpServer (!SettingUtil .getSwitchEnableHttpServer ());
@@ -138,23 +156,24 @@ protected void onStart() {
138
156
}
139
157
});
140
158
159
+ //接收
141
160
receiveBtn .setOnClickListener (v -> {
142
161
if (HttpServer .asRunning ()) {
143
162
receiveTxt .setText (R .string .sender_cannot_receive );
144
- Toast ( handError , TAG , getString (R .string .sender_cannot_receive ));
163
+ ToastUtils . show ( getString (R .string .sender_cannot_receive ));
145
164
return ;
146
165
}
147
166
148
167
if (NetUtil .NETWORK_WIFI != NetUtil .getNetWorkStatus ()) {
149
168
receiveTxt .setText (R .string .no_wifi_network );
150
- Toast ( handError , TAG , getString (R .string .no_wifi_network ));
169
+ ToastUtils . show ( getString (R .string .no_wifi_network ));
151
170
return ;
152
171
}
153
172
154
173
serverIp = textServerIp .getIP ();
155
174
if (serverIp == null || serverIp .isEmpty ()) {
156
175
receiveTxt .setText (R .string .invalid_server_ip );
157
- Toast ( handError , TAG , getString (R .string .invalid_server_ip ));
176
+ ToastUtils . show ( getString (R .string .invalid_server_ip ));
158
177
return ;
159
178
}
160
179
@@ -187,7 +206,7 @@ protected void onStart() {
187
206
client .newCall (request ).enqueue (new Callback () {
188
207
@ Override
189
208
public void onFailure (@ NonNull Call call , @ NonNull final IOException e ) {
190
- Toast ( handError , TAG , getString (R .string .tips_get_info_failed ));
209
+ ToastUtils . show ( getString (R .string .tips_get_info_failed ));
191
210
}
192
211
193
212
@ Override
@@ -196,7 +215,7 @@ public void onResponse(@NonNull Call call, @NonNull Response response) throws IO
196
215
Log .d (TAG , "Response:" + response .code () + "," + responseStr );
197
216
198
217
if (TextUtils .isEmpty (responseStr )) {
199
- Toast ( handError , TAG , getString (R .string .tips_get_info_failed ));
218
+ ToastUtils . show ( getString (R .string .tips_get_info_failed ));
200
219
return ;
201
220
}
202
221
@@ -205,27 +224,68 @@ public void onResponse(@NonNull Call call, @NonNull Response response) throws IO
205
224
Log .d (TAG , cloneInfoVo .toString ());
206
225
207
226
if (!SettingUtil .getVersionName ().equals (cloneInfoVo .getVersionName ())) {
208
- Toast ( handError , TAG , getString (R .string .tips_versions_inconsistent ));
227
+ ToastUtils . show ( getString (R .string .tips_versions_inconsistent ));
209
228
return ;
210
229
}
211
230
212
- //下载备份文件
213
- Message msg = new Message ();
214
- msg .what = DOWNLOAD ;
215
- Bundle bundle = new Bundle ();
216
- bundle .putString ("URL" , requestUrl );
217
- bundle .putString ("INFO" , responseStr );
218
- msg .setData (bundle );
219
- handError .sendMessage (msg );
231
+ if (CloneUtils .restoreSettings (cloneInfoVo )) {
232
+ ToastUtils .show (getString (R .string .tips_clone_done ));
233
+ } else {
234
+ ToastUtils .show (getString (R .string .tips_clone_failed ));
235
+ }
220
236
221
237
} catch (Exception e ) {
222
- Toast ( handError , TAG , getString (R .string .tips_clone_failed ) + e .getMessage ());
238
+ ToastUtils . show ( getString (R .string .tips_clone_failed ) + e .getMessage ());
223
239
}
224
240
}
225
241
});
226
242
227
243
});
228
244
245
+ Button exportBtn = findViewById (R .id .exportBtn );
246
+ TextView exportTxt = findViewById (R .id .exportTxt );
247
+ Button importBtn = findViewById (R .id .importBtn );
248
+ TextView importTxt = findViewById (R .id .importTxt );
249
+
250
+ //导出
251
+ exportBtn .setOnClickListener (v -> {
252
+ if (FileUtils .writeFileR (CloneUtils .exportSettings (), backupPath , backupFile , true )) {
253
+ ToastUtils .show ("导出配置成功!" );
254
+ } else {
255
+ exportTxt .setText ("导出失败,请检查写入权限!" );
256
+ ToastUtils .show ("导出失败,请检查写入权限!" );
257
+ }
258
+ });
259
+
260
+ //导入
261
+ importBtn .setOnClickListener (v -> {
262
+ try {
263
+ String responseStr = FileUtils .readFileI (backupPath , backupFile );
264
+ if (TextUtils .isEmpty (responseStr )) {
265
+ ToastUtils .show (getString (R .string .tips_get_info_failed ));
266
+ return ;
267
+ }
268
+
269
+ CloneInfoVo cloneInfoVo = JSON .parseObject (responseStr , CloneInfoVo .class );
270
+ Log .d (TAG , Objects .requireNonNull (cloneInfoVo ).toString ());
271
+
272
+ if (!SettingUtil .getVersionName ().equals (cloneInfoVo .getVersionName ())) {
273
+ ToastUtils .show (getString (R .string .tips_versions_inconsistent ));
274
+ return ;
275
+ }
276
+
277
+ if (CloneUtils .restoreSettings (cloneInfoVo )) {
278
+ ToastUtils .show (getString (R .string .tips_clone_done ));
279
+ } else {
280
+ ToastUtils .show (getString (R .string .tips_clone_failed ));
281
+ }
282
+
283
+ } catch (Exception e ) {
284
+ e .printStackTrace ();
285
+ importTxt .setText ("还原失败:" + e .getMessage ());
286
+ }
287
+ });
288
+
229
289
}
230
290
231
291
@ SuppressLint ("SetTextI18n" )
@@ -238,82 +298,4 @@ protected void onResume() {
238
298
ipText .setText (getString (R .string .local_ip ) + serverIp );
239
299
}
240
300
241
- /**
242
- * 文件下载
243
- *
244
- * @param url 下载链接
245
- */
246
- public void downloadFile (String url , final String destFileDir , final String destFileName , final String cloneInfo ) {
247
- ProgressDialog progressDialog = new ProgressDialog (context );
248
- progressDialog .setProgressStyle (ProgressDialog .STYLE_HORIZONTAL );
249
- progressDialog .setTitle (getString (R .string .tips_downloading ));
250
- progressDialog .setMessage (getString (R .string .tips_please_wait ));
251
- progressDialog .setProgress (0 );
252
- progressDialog .setMax (100 );
253
- progressDialog .show ();
254
- progressDialog .setCancelable (false );
255
- DownloadUtil .get ().download (url , destFileDir , destFileName , new DownloadUtil .OnDownloadListener () {
256
- @ Override
257
- public void onDownloadSuccess (File file ) {
258
- if (progressDialog .isShowing ()) {
259
- Toast (handError , TAG , getString (R .string .tips_download_done ));
260
- progressDialog .dismiss ();
261
- }
262
- //下载完成进行相关逻辑操作
263
- Log .d (TAG , file .getPath ());
264
-
265
- //还原数据库
266
- BackupDbTask task = new BackupDbTask (context );
267
- String backup_version = task .doInBackground (BackupDbTask .COMMAND_RESTORE );
268
- Log .d (TAG , "backup_version = " + backup_version );
269
-
270
- //应用配置
271
- CloneInfoVo cloneInfoVo = JSON .parseObject (cloneInfo , CloneInfoVo .class );
272
- System .out .println (cloneInfoVo .toString ());
273
- SettingUtil .init (context );
274
- SettingUtil .switchEnableSms (cloneInfoVo .isEnableSms ());
275
- SettingUtil .switchEnablePhone (cloneInfoVo .isEnablePhone ());
276
- SettingUtil .switchCallType1 (cloneInfoVo .isCallType1 ());
277
- SettingUtil .switchCallType2 (cloneInfoVo .isCallType2 ());
278
- SettingUtil .switchCallType3 (cloneInfoVo .isCallType3 ());
279
- SettingUtil .switchEnableAppNotify (cloneInfoVo .isEnableAppNotify ());
280
- SettingUtil .switchCancelAppNotify (cloneInfoVo .isCancelAppNotify ());
281
- SettingUtil .smsHubApiUrl (cloneInfoVo .getSmsHubApiUrl ());
282
- SettingUtil .setBatteryLevelAlarmMin (cloneInfoVo .getBatteryLevelAlarmMin ());
283
- SettingUtil .setBatteryLevelAlarmMax (cloneInfoVo .getBatteryLevelAlarmMax ());
284
- SettingUtil .switchBatteryLevelAlarmOnce (cloneInfoVo .isBatteryLevelAlarmOnce ());
285
- SettingUtil .setRetryTimes (cloneInfoVo .getRetryTimes ());
286
- SettingUtil .setDelayTime (cloneInfoVo .getDelayTime ());
287
- SettingUtil .switchSmsTemplate (cloneInfoVo .isEnableSmsTemplate ());
288
- SettingUtil .setSmsTemplate (cloneInfoVo .getSmsTemplate ());
289
-
290
- Toast (handError , TAG , getString (R .string .tips_clone_done ));
291
- }
292
-
293
- @ Override
294
- public void onDownloading (int progress ) {
295
- progressDialog .setProgress (progress );
296
- }
297
-
298
- @ SuppressLint ("SetTextI18n" )
299
- @ Override
300
- public void onDownloadFailed (Exception e ) {
301
- //下载异常进行相关提示操作
302
- Log .e (TAG , getString (R .string .tips_download_failed ) + e .getMessage ());
303
- Toast (handError , TAG , getString (R .string .tips_download_failed ) + e .getMessage ());
304
- }
305
- });
306
- }
307
-
308
- public static void Toast (Handler handError , String Tag , String data ) {
309
- Log .i (Tag , data );
310
- if (handError != null ) {
311
- Message msg = new Message ();
312
- msg .what = TOAST ;
313
- Bundle bundle = new Bundle ();
314
- bundle .putString ("DATA" , data );
315
- msg .setData (bundle );
316
- handError .sendMessage (msg );
317
- }
318
- }
319
301
}
0 commit comments