Skip to content

Commit

Permalink
feat:导入导出绑定的电脑列表数据库DB文件,适合在新设备端无需重新配对使用。
Browse files Browse the repository at this point in the history
  • Loading branch information
Axixi2233 committed Jul 25, 2024
1 parent ea2bea5 commit 73b1372
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdk 21
targetSdk 34

versionName "12.1-240719-axi"
versionName "12.1-240725-axi"
versionCode = 314

// Generate native debug symbols to allow Google Play to symbolicate our native crashes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.limelight.computers;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
Expand All @@ -23,7 +24,7 @@
import org.json.JSONObject;

public class ComputerDatabaseManager {
private static final String COMPUTER_DB_NAME = "computers4.db";
public static final String COMPUTER_DB_NAME = "computers4.db";
private static final String COMPUTER_TABLE_NAME = "Computers";
private static final String COMPUTER_UUID_COLUMN_NAME = "UUID";
private static final String COMPUTER_NAME_COLUMN_NAME = "ComputerName";
Expand Down Expand Up @@ -55,6 +56,12 @@ public ComputerDatabaseManager(Context c) {
initializeDb(c);
}

public ComputerDatabaseManager(Context context, File file){
computerDb=SQLiteDatabase.openDatabase(file.getPath(),
null, SQLiteDatabase.OPEN_READONLY);
}


public void close() {
computerDb.close();
}
Expand Down
66 changes: 63 additions & 3 deletions app/src/main/java/com/limelight/preferences/StreamSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import android.support.v4.content.FileProvider;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.NumberKeyListener;
import android.util.DisplayMetrics;
import android.util.Range;
import android.view.Display;
Expand All @@ -45,14 +43,16 @@
import com.limelight.R;
import com.limelight.binding.input.virtual_controller.keyboard.KeyBoardControllerConfigurationLoader;
import com.limelight.binding.video.MediaCodecHelper;
import com.limelight.computers.ComputerDatabaseManager;
import com.limelight.nvstream.http.ComputerDetails;
import com.limelight.utils.Dialog;
import com.limelight.utils.FileUriUtils;
import com.limelight.utils.UiHelper;
import org.json.JSONObject;
import java.io.File;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class StreamSettings extends Activity {
Expand Down Expand Up @@ -709,6 +709,18 @@ public boolean onPreferenceClick(Preference preference) {
return false;
}
});

findPreference("import_computers_data_file").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, READ_DATABASE_REQUEST_CODE);
return false;
}
});

findPreference("import_special_button_file").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Expand Down Expand Up @@ -745,6 +757,25 @@ public boolean onPreferenceClick(Preference preference) {
}
});

findPreference("export_computers_data_file").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
File dataFile=new File(getActivity().getDatabasePath(ComputerDatabaseManager.COMPUTER_DB_NAME).getPath());
if(!dataFile.exists()){
return false;
}
Uri uri;
Intent intent = new Intent(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String authority= BuildConfig.APPLICATION_ID+".fileprovider";
uri= FileProvider.getUriForFile(getActivity(),authority,dataFile);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("*/*");
startActivity(Intent.createChooser(intent,"保存数据文件"));
return false;
}
});


findPreference("pref_axi_test").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
Expand Down Expand Up @@ -807,6 +838,9 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}
int READ_REQUEST_CODE=1001;
int READ_REQUEST_SPECIAL_CODE=1002;

int READ_DATABASE_REQUEST_CODE=1003;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand Down Expand Up @@ -853,6 +887,32 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
e.printStackTrace();
Toast.makeText(getActivity(),"出错啦~"+e.getMessage(),Toast.LENGTH_SHORT).show();
}
return;
}

if (requestCode == READ_DATABASE_REQUEST_CODE && resultCode == Activity.RESULT_OK &&data.getData()!=null) {
try {
Uri uri = data.getData();
File dataBaseFile= null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
dataBaseFile = FileUriUtils.uriToFileApiQ(uri,getActivity());
}else{
String displayName = System.currentTimeMillis() + Math.round((Math.random() + 1) * 1000)+".db";
dataBaseFile=new File(getActivity().getCacheDir().getAbsolutePath(), displayName);
FileUriUtils.copyUriToInternalStorage(getActivity(),uri,dataBaseFile);
}
ComputerDatabaseManager importManager=new ComputerDatabaseManager(getActivity(),dataBaseFile);
List<ComputerDetails> importComputers=importManager.getAllComputers();
ComputerDatabaseManager manager=new ComputerDatabaseManager(getActivity());
for (ComputerDetails computer : importComputers) {
manager.updateComputer(computer);
}
Toast.makeText(getActivity(),"导入成功,重新打开APP生效!",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(),"出错啦~"+e.getMessage(),Toast.LENGTH_SHORT).show();
}
return;
}
}

Expand Down
68 changes: 67 additions & 1 deletion app/src/main/java/com/limelight/utils/FileUriUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.limelight.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;

import android.os.Build;
import android.os.FileUtils;
import android.support.annotation.RequiresApi;
import android.webkit.MimeTypeMap;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -99,4 +103,66 @@ public static boolean writerFileString(File file, String content) {
}
return true;
}

@RequiresApi(api = Build.VERSION_CODES.Q)
public static File uriToFileApiQ(Uri uri, Context context) {
File file = null;
if (uri == null) return file;
//android10以上转换
if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
file = new File(uri.getPath());
} else if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
//把文件复制到沙盒目录
ContentResolver contentResolver = context.getContentResolver();
String displayName = System.currentTimeMillis() + Math.round((Math.random() + 1) * 1000)
+ "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(contentResolver.getType(uri));
try {
InputStream is = contentResolver.openInputStream(uri);
File cache = new File(context.getCacheDir().getAbsolutePath(), displayName);
FileOutputStream fos = new FileOutputStream(cache);
FileUtils.copy(is, fos);
file = cache;
fos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}

public static void copyUriToInternalStorage(Context context, Uri uri, File destFile) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
// 从Uri获取输入流
inputStream = context.getContentResolver().openInputStream(uri);
if (inputStream == null) {
return;
}

// 创建目标文件
outputStream = new FileOutputStream(destFile);

// 缓冲区大小
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}

outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭输入流和输出流
try {
if (inputStream != null) inputStream.close();
if (outputStream != null) outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
14 changes: 12 additions & 2 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,6 @@
android:text="%"
android:title="屏幕灵敏度调整Y轴" />

checkbox_enable_touch_sensitivity_rotation_auto

<Preference
android:key="pref_axi_test"
android:title="阿西西调试页面"
Expand Down Expand Up @@ -517,6 +515,18 @@
android:summary="每次都会覆盖原来的,请注意备份。"
/>

<Preference
android:key="export_computers_data_file"
android:title="导出配对的电脑数据库文件"
android:summary="导出一个db格式的sqlite数据库文件"
/>

<Preference
android:key="import_computers_data_file"
android:title="导入配对的电脑数据库文件"
android:summary="选择db数据库文件,会覆盖更新原有的电脑列表,需要重新打开APP生效。"
/>

<com.limelight.preferences.WebLauncherPreference
android:title="软件版本更新"
android:summary="跳转到github获取releases"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/xml/provider_file_paths.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
<files-path
name="files"
path="." />
<root-path name="root" path="." />
</paths>

0 comments on commit 73b1372

Please sign in to comment.