Skip to content

Commit f433d7f

Browse files
committed
完成 基于Kotlin+协程+MVVM的重构(还剩一个剪切板的UI展示)
1 parent 0317188 commit f433d7f

File tree

13 files changed

+569
-118
lines changed

13 files changed

+569
-118
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<!-- <meta-data android:name="firebase_analytics_collection_deactivated" android:value="true" />-->
2121

2222
<application
23+
android:name=".MyApp"
2324
android:allowBackup="true"
2425
android:icon="@mipmap/ic_launcher"
2526
android:label="@string/app_name"

app/src/main/java/com/sibyl/httpfiledominator/HttpServerConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ private void shareOneFile(DataOutputStream output, Boolean sendOnlyHeader, Strin
248248
if (!sendOnlyHeader) {
249249

250250
if (theUriInterpretation.isDirectory() || fileUriZ.size() > 1) {
251-
FileZipper zz = new FileZipper(output, fileUriZ, launcherActivity.getContentResolver());
251+
FileZipper zz = new FileZipper(output, fileUriZ, MyApp.getInstance().getContentResolver());
252252
zz.run();
253253
} else {
254254
byte[] buffer = new byte[8 * 1024 * 1024];//8M
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.sibyl.httpfiledominator
2+
3+
import android.app.Application
4+
5+
/**
6+
* @author Sasuke on 2020/6/23.
7+
*/
8+
class MyApp : Application(){
9+
companion object{
10+
@JvmStatic
11+
var instance: MyApp? = null
12+
}
13+
14+
override fun onCreate() {
15+
super.onCreate()
16+
instance = this
17+
}
18+
}

app/src/main/java/com/sibyl/httpfiledominator/MyHttpServer.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
public class MyHttpServer extends Thread {
6969

7070
// by design, we only serve one file at a time.
71-
7271
private static final ExecutorService threadPool = Executors.newCachedThreadPool();
7372
private static int port;
7473
private static ArrayList<UriInterpretation> fileUris = new ArrayList<>();//主列表
@@ -128,17 +127,6 @@ public static void setClipboardUris(ArrayList<UriInterpretation> newUris){
128127
MyHttpServer.clipboardUris = newUris;
129128
}
130129

131-
/**根据类型来传uri们*/
132-
public static void addAllUrisByMode(boolean isClipboardMode, ArrayList<UriInterpretation> newUris) {
133-
if (isClipboardMode) {
134-
clipboardUris.addAll(newUris);
135-
fileUris = clipboardUris;
136-
} else {
137-
normalUris.addAll(newUris);
138-
fileUris = normalUris;
139-
}
140-
}
141-
142130
public static void changeUrisByMode(boolean isClipboardMode){
143131
fileUris = isClipboardMode? clipboardUris : normalUris;
144132
}
@@ -215,7 +203,7 @@ public synchronized void stopServer() {
215203
}
216204
}
217205

218-
public CharSequence[] listOfIpAddresses() {
206+
public ArrayList<String> listOfIpAddresses() {
219207
ArrayList<String> arrayOfIps = new ArrayList<String>();
220208

221209

@@ -259,9 +247,9 @@ public int compare(String o1, String o2) {
259247
}
260248
});
261249

262-
CharSequence[] output = arrayOfIps.toArray(new CharSequence[arrayOfIps
263-
.size()]);
264-
return output;
250+
// CharSequence[] output = arrayOfIps.toArray(new CharSequence[arrayOfIps
251+
// .size()]);
252+
return arrayOfIps;
265253
}
266254

267255
private boolean normalBind(int thePort) {
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
package com.sibyl.httpfiledominator.activities
22

3+
import android.os.Bundle
34
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.appcompat.widget.Toolbar
6+
import com.sibyl.httpfiledominator.R
47

58
/**
69
* @author Sasuke on 2020/6/23.
710
*/
8-
class BaseActivity: AppCompatActivity() {
11+
open class BaseActivity: AppCompatActivity() {
12+
companion object{
13+
val HANDLER_CONNECTION_START = 42
14+
val HANDLER_CONNECTION_END = 4242
15+
}
16+
17+
18+
override fun onCreate(savedInstanceState: Bundle?) {
19+
super.onCreate(savedInstanceState)
20+
}
21+
22+
fun initUIAfter(toolbar: Toolbar){
23+
window.navigationBarColor = resources.getColor(R.color.main_activity_background_color, null)
24+
//设置ActionBar
25+
setSupportActionBar(toolbar.apply {
26+
setTitle(getString(R.string.app_name))
27+
setTitleTextColor(getResources().getColor(R.color.light_blue, null))
28+
})
29+
}
930
}

app/src/main/java/com/sibyl/httpfiledominator/activities/MainActivity0.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,7 @@ private ArrayList<UriInterpretation> getIntentFileUris(Intent dataIntent) {
412412
}
413413

414414
private ArrayList<UriInterpretation> getUrisForActionSendMultiple(Intent dataIntent, ArrayList<UriInterpretation> theUris) {
415-
ArrayList<Parcelable> list = dataIntent
416-
.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
415+
ArrayList<Parcelable> list = dataIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
417416
if (list != null) {
418417
for (Parcelable parcelable : list) {
419418
Uri stream = (Uri) parcelable;
@@ -506,7 +505,8 @@ protected void initHttpServer(ArrayList<UriInterpretation> myUris) {
506505
}
507506
notiDominator.showNotifi();
508507
httpServer = new MyHttpServer(1120);
509-
listOfServerUris = httpServer.listOfIpAddresses();
508+
//注释掉防报错
509+
// listOfServerUris = httpServer.listOfIpAddresses();
510510
preferredServerUrl = listOfServerUris[0].toString();
511511

512512
showIPText();
Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,84 @@
11
package com.sibyl.httpfiledominator.mainactivity.model
22

3+
import android.content.Intent
4+
import android.os.Handler
5+
import android.os.Looper
6+
import androidx.databinding.ObservableField
7+
import androidx.lifecycle.MutableLiveData
38
import androidx.lifecycle.ViewModel
9+
import androidx.lifecycle.viewModelScope
10+
import com.sibyl.httpfiledominator.MyHttpServer
11+
import com.sibyl.httpfiledominator.UriInterpretation
412
import com.sibyl.httpfiledominator.mainactivity.repo.MainRepo
13+
import kotlinx.coroutines.launch
514

615
/**
716
* @author HUANGSHI-PC on 2020-03-06 0006.
817
*/
9-
class MainModel(val repo: MainRepo): ViewModel() {
18+
class MainModel(val repo: MainRepo) : ViewModel() {
19+
val isLoading = MutableLiveData<Boolean>().apply { value = false }
20+
21+
val snackbarMsg = MutableLiveData<String>().apply { value = "" }
22+
23+
val httpServer = MutableLiveData<MyHttpServer?>()
24+
/**默认显示的IP地址*/
25+
val preferredServerUrl = ObservableField<String>()
26+
/**服务器IP地址集*/
27+
val listOfServerUris = MutableLiveData<MutableList<String>>()
28+
29+
/**每次加的新数据缓存在这里*/
30+
val newUrisCache = MutableLiveData<List<UriInterpretation>>()
31+
32+
/**是否剪切板模式*/
33+
val isClipboardMode = MutableLiveData<Boolean>().apply { value = false }
34+
35+
/**处理刚进页面时传入的Intent*/
36+
fun dealWithNewIntent(intent: Intent?) = viewModelScope.launch {
37+
// if (isFinishing()) conti.resumeWithException(Exception(""))
38+
if (intent?.extras != null) isLoading.value = true
39+
try {
40+
intent?.extras?.let {
41+
val sharedUriList = repo.getIntentFileUris(intent)
42+
dealWithNewUris(sharedUriList)
43+
}
44+
} catch (e: Exception) {
45+
snackbarMsg.value = e.message
46+
}finally {
47+
isLoading.value = false
48+
}
49+
}
50+
51+
/**处理ActivityResult传入的Intent*/
52+
fun dealActivityResultIntent(intent: Intent?) = viewModelScope.launch {
53+
if (intent != null) isLoading.value = true
54+
try {
55+
val newUriList = repo.getActivityResultUris(intent)
56+
dealWithNewUris(newUriList)
57+
} catch (e: Exception) {
58+
snackbarMsg.value = e.message
59+
}finally {
60+
isLoading.value = false
61+
}
62+
}
63+
64+
65+
66+
/**对新uri的处理*/
67+
fun dealWithNewUris(newUriList: MutableList<UriInterpretation>?) = viewModelScope.launch {
68+
//先过滤一波已经添加过的
69+
newUriList?.removeAll(MyHttpServer.getNormalUris())
70+
if (newUriList.isNullOrEmpty()) return@launch
71+
//如果没有建立起http服务时==========
72+
if (httpServer.value == null){
73+
httpServer.value = MyHttpServer(1120)
74+
}
75+
MyHttpServer.getNormalUris().addAll(newUriList)
76+
//显示到UI
77+
newUrisCache.value = newUriList
78+
MyHttpServer.changeUrisByMode(false)
79+
//切换模式
80+
isClipboardMode.value = false
81+
}
82+
1083

1184
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,63 @@
11
package com.sibyl.httpfiledominator.mainactivity.repo
22

3+
import android.content.Intent
4+
import android.net.Uri
5+
import android.os.Parcelable
6+
import com.sibyl.httpfiledominator.MyApp
7+
import com.sibyl.httpfiledominator.UriInterpretation
8+
import kotlinx.coroutines.Dispatchers
9+
import kotlinx.coroutines.withContext
10+
311
/**
412
* @author HUANGSHI-PC on 2020-03-06 0006.
513
*/
614
class MainRepo {
15+
16+
/**
17+
* 以下是从SendFileActivity里拷过来的。用来接收直接从外面分享进来的文件,
18+
* 原SendFileActivity已废弃
19+
*/
20+
suspend fun getIntentFileUris(dataIntent: Intent): MutableList<UriInterpretation>? = withContext(Dispatchers.IO) {
21+
val theUris = mutableListOf<UriInterpretation>()
22+
if (Intent.ACTION_SEND_MULTIPLE == dataIntent.action) {
23+
dataIntent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.forEach {
24+
it?.let { theUris.add(UriInterpretation(it as Uri, MyApp.instance?.contentResolver)) }
25+
}
26+
return@withContext theUris
27+
}
28+
val extras = dataIntent.extras
29+
if (extras == null) {//直接手动进的主页,不会存在传intent数据的情况
30+
throw Exception()
31+
}
32+
var myUri: Uri? = extras[Intent.EXTRA_STREAM] as Uri
33+
if (myUri == null) {
34+
val tempString = extras[Intent.EXTRA_TEXT] as String?
35+
if (tempString == null) {
36+
throw Exception("Error obtaining the file path")
37+
}
38+
myUri = Uri.parse(tempString)
39+
if (myUri == null) {
40+
throw Exception("Error obtaining the file path")
41+
}
42+
}
43+
theUris.add(UriInterpretation(myUri, MyApp.instance?.getContentResolver()))
44+
theUris
45+
}
46+
47+
/**处理onActivityResult()里的新数据*/
48+
suspend fun getActivityResultUris(data: Intent?): MutableList<UriInterpretation> = withContext(Dispatchers.IO) {
49+
val theUris = mutableListOf<UriInterpretation>()
50+
val dataUri = data?.getData()
51+
if (dataUri != null) {
52+
theUris.add(UriInterpretation(dataUri, MyApp.instance?.getContentResolver()))
53+
} else {
54+
val clipData = data?.getClipData()
55+
for (i in 0 until (clipData?.itemCount ?: 0)) {
56+
val item = clipData?.getItemAt(i)
57+
val uri = item?.uri
58+
theUris.add(UriInterpretation(uri, MyApp.instance?.getContentResolver()))
59+
}
60+
}
61+
theUris
62+
}
763
}

0 commit comments

Comments
 (0)