Skip to content

Commit

Permalink
完成 基于Kotlin+协程+MVVM的重构(还剩一个剪切板的UI展示)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheetah747 committed Jun 24, 2020
1 parent 0317188 commit f433d7f
Show file tree
Hide file tree
Showing 13 changed files with 569 additions and 118 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<!-- <meta-data android:name="firebase_analytics_collection_deactivated" android:value="true" />-->

<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private void shareOneFile(DataOutputStream output, Boolean sendOnlyHeader, Strin
if (!sendOnlyHeader) {

if (theUriInterpretation.isDirectory() || fileUriZ.size() > 1) {
FileZipper zz = new FileZipper(output, fileUriZ, launcherActivity.getContentResolver());
FileZipper zz = new FileZipper(output, fileUriZ, MyApp.getInstance().getContentResolver());
zz.run();
} else {
byte[] buffer = new byte[8 * 1024 * 1024];//8M
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/sibyl/httpfiledominator/MyApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.sibyl.httpfiledominator

import android.app.Application

/**
* @author Sasuke on 2020/6/23.
*/
class MyApp : Application(){
companion object{
@JvmStatic
var instance: MyApp? = null
}

override fun onCreate() {
super.onCreate()
instance = this
}
}
20 changes: 4 additions & 16 deletions app/src/main/java/com/sibyl/httpfiledominator/MyHttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
public class MyHttpServer extends Thread {

// by design, we only serve one file at a time.

private static final ExecutorService threadPool = Executors.newCachedThreadPool();
private static int port;
private static ArrayList<UriInterpretation> fileUris = new ArrayList<>();//主列表
Expand Down Expand Up @@ -128,17 +127,6 @@ public static void setClipboardUris(ArrayList<UriInterpretation> newUris){
MyHttpServer.clipboardUris = newUris;
}

/**根据类型来传uri们*/
public static void addAllUrisByMode(boolean isClipboardMode, ArrayList<UriInterpretation> newUris) {
if (isClipboardMode) {
clipboardUris.addAll(newUris);
fileUris = clipboardUris;
} else {
normalUris.addAll(newUris);
fileUris = normalUris;
}
}

public static void changeUrisByMode(boolean isClipboardMode){
fileUris = isClipboardMode? clipboardUris : normalUris;
}
Expand Down Expand Up @@ -215,7 +203,7 @@ public synchronized void stopServer() {
}
}

public CharSequence[] listOfIpAddresses() {
public ArrayList<String> listOfIpAddresses() {
ArrayList<String> arrayOfIps = new ArrayList<String>();


Expand Down Expand Up @@ -259,9 +247,9 @@ public int compare(String o1, String o2) {
}
});

CharSequence[] output = arrayOfIps.toArray(new CharSequence[arrayOfIps
.size()]);
return output;
// CharSequence[] output = arrayOfIps.toArray(new CharSequence[arrayOfIps
// .size()]);
return arrayOfIps;
}

private boolean normalBind(int thePort) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
package com.sibyl.httpfiledominator.activities

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.sibyl.httpfiledominator.R

/**
* @author Sasuke on 2020/6/23.
*/
class BaseActivity: AppCompatActivity() {
open class BaseActivity: AppCompatActivity() {
companion object{
val HANDLER_CONNECTION_START = 42
val HANDLER_CONNECTION_END = 4242
}


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

fun initUIAfter(toolbar: Toolbar){
window.navigationBarColor = resources.getColor(R.color.main_activity_background_color, null)
//设置ActionBar
setSupportActionBar(toolbar.apply {
setTitle(getString(R.string.app_name))
setTitleTextColor(getResources().getColor(R.color.light_blue, null))
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@ private ArrayList<UriInterpretation> getIntentFileUris(Intent dataIntent) {
}

private ArrayList<UriInterpretation> getUrisForActionSendMultiple(Intent dataIntent, ArrayList<UriInterpretation> theUris) {
ArrayList<Parcelable> list = dataIntent
.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
ArrayList<Parcelable> list = dataIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (list != null) {
for (Parcelable parcelable : list) {
Uri stream = (Uri) parcelable;
Expand Down Expand Up @@ -506,7 +505,8 @@ protected void initHttpServer(ArrayList<UriInterpretation> myUris) {
}
notiDominator.showNotifi();
httpServer = new MyHttpServer(1120);
listOfServerUris = httpServer.listOfIpAddresses();
//注释掉防报错
// listOfServerUris = httpServer.listOfIpAddresses();
preferredServerUrl = listOfServerUris[0].toString();

showIPText();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,84 @@
package com.sibyl.httpfiledominator.mainactivity.model

import android.content.Intent
import android.os.Handler
import android.os.Looper
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sibyl.httpfiledominator.MyHttpServer
import com.sibyl.httpfiledominator.UriInterpretation
import com.sibyl.httpfiledominator.mainactivity.repo.MainRepo
import kotlinx.coroutines.launch

/**
* @author HUANGSHI-PC on 2020-03-06 0006.
*/
class MainModel(val repo: MainRepo): ViewModel() {
class MainModel(val repo: MainRepo) : ViewModel() {
val isLoading = MutableLiveData<Boolean>().apply { value = false }

val snackbarMsg = MutableLiveData<String>().apply { value = "" }

val httpServer = MutableLiveData<MyHttpServer?>()
/**默认显示的IP地址*/
val preferredServerUrl = ObservableField<String>()
/**服务器IP地址集*/
val listOfServerUris = MutableLiveData<MutableList<String>>()

/**每次加的新数据缓存在这里*/
val newUrisCache = MutableLiveData<List<UriInterpretation>>()

/**是否剪切板模式*/
val isClipboardMode = MutableLiveData<Boolean>().apply { value = false }

/**处理刚进页面时传入的Intent*/
fun dealWithNewIntent(intent: Intent?) = viewModelScope.launch {
// if (isFinishing()) conti.resumeWithException(Exception(""))
if (intent?.extras != null) isLoading.value = true
try {
intent?.extras?.let {
val sharedUriList = repo.getIntentFileUris(intent)
dealWithNewUris(sharedUriList)
}
} catch (e: Exception) {
snackbarMsg.value = e.message
}finally {
isLoading.value = false
}
}

/**处理ActivityResult传入的Intent*/
fun dealActivityResultIntent(intent: Intent?) = viewModelScope.launch {
if (intent != null) isLoading.value = true
try {
val newUriList = repo.getActivityResultUris(intent)
dealWithNewUris(newUriList)
} catch (e: Exception) {
snackbarMsg.value = e.message
}finally {
isLoading.value = false
}
}



/**对新uri的处理*/
fun dealWithNewUris(newUriList: MutableList<UriInterpretation>?) = viewModelScope.launch {
//先过滤一波已经添加过的
newUriList?.removeAll(MyHttpServer.getNormalUris())
if (newUriList.isNullOrEmpty()) return@launch
//如果没有建立起http服务时==========
if (httpServer.value == null){
httpServer.value = MyHttpServer(1120)
}
MyHttpServer.getNormalUris().addAll(newUriList)
//显示到UI
newUrisCache.value = newUriList
MyHttpServer.changeUrisByMode(false)
//切换模式
isClipboardMode.value = false
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,63 @@
package com.sibyl.httpfiledominator.mainactivity.repo

import android.content.Intent
import android.net.Uri
import android.os.Parcelable
import com.sibyl.httpfiledominator.MyApp
import com.sibyl.httpfiledominator.UriInterpretation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
* @author HUANGSHI-PC on 2020-03-06 0006.
*/
class MainRepo {

/**
* 以下是从SendFileActivity里拷过来的。用来接收直接从外面分享进来的文件,
* 原SendFileActivity已废弃
*/
suspend fun getIntentFileUris(dataIntent: Intent): MutableList<UriInterpretation>? = withContext(Dispatchers.IO) {
val theUris = mutableListOf<UriInterpretation>()
if (Intent.ACTION_SEND_MULTIPLE == dataIntent.action) {
dataIntent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.forEach {
it?.let { theUris.add(UriInterpretation(it as Uri, MyApp.instance?.contentResolver)) }
}
return@withContext theUris
}
val extras = dataIntent.extras
if (extras == null) {//直接手动进的主页,不会存在传intent数据的情况
throw Exception()
}
var myUri: Uri? = extras[Intent.EXTRA_STREAM] as Uri
if (myUri == null) {
val tempString = extras[Intent.EXTRA_TEXT] as String?
if (tempString == null) {
throw Exception("Error obtaining the file path")
}
myUri = Uri.parse(tempString)
if (myUri == null) {
throw Exception("Error obtaining the file path")
}
}
theUris.add(UriInterpretation(myUri, MyApp.instance?.getContentResolver()))
theUris
}

/**处理onActivityResult()里的新数据*/
suspend fun getActivityResultUris(data: Intent?): MutableList<UriInterpretation> = withContext(Dispatchers.IO) {
val theUris = mutableListOf<UriInterpretation>()
val dataUri = data?.getData()
if (dataUri != null) {
theUris.add(UriInterpretation(dataUri, MyApp.instance?.getContentResolver()))
} else {
val clipData = data?.getClipData()
for (i in 0 until (clipData?.itemCount ?: 0)) {
val item = clipData?.getItemAt(i)
val uri = item?.uri
theUris.add(UriInterpretation(uri, MyApp.instance?.getContentResolver()))
}
}
theUris
}
}
Loading

0 comments on commit f433d7f

Please sign in to comment.