mirror of
https://github.com/keymanapp/keyman.git
synced 2026-09-22 15:47:41 +00:00
chore(android): Refactor HelpFile as utility
This commit is contained in:
parent
6cc8a56681
commit
c772a2dbfe
3 changed files with 99 additions and 80 deletions
|
|
@ -4,16 +4,11 @@
|
|||
|
||||
package com.tavultesoft.kmea;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.core.content.FileProvider;
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipDescription;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
|
|
@ -33,8 +28,8 @@ import android.widget.SimpleAdapter;
|
|||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.tavultesoft.kmea.util.FileUtils;
|
||||
import com.tavultesoft.kmea.util.FileProviderUtils;
|
||||
import com.tavultesoft.kmea.util.HelpFile;
|
||||
import com.tavultesoft.kmea.util.MapCompat;
|
||||
import com.tavultesoft.kmea.util.QRCodeUtil;
|
||||
|
||||
|
|
@ -128,67 +123,15 @@ public final class KeyboardInfoActivity extends AppCompatActivity {
|
|||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
if (position == 1) {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
||||
|
||||
if (customHelpLink != null) {
|
||||
// Display local welcome.htm help file, including associated assets
|
||||
if (FileUtils.isWelcomeFile(customHelpLink) && ! KMManager.isTestMode()) {
|
||||
File customHelp = new File(new File(customHelpLink).getAbsolutePath());
|
||||
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
// Starting with Android N, you can't pass file:// to intents, so we use FileProvider
|
||||
try {
|
||||
final String authority = FileProviderUtils.getAuthority(context);
|
||||
Uri contentUri = FileProvider.getUriForFile(
|
||||
context, authority, customHelp);
|
||||
i.setDataAndType(contentUri, "text/html");
|
||||
Intent i = HelpFile.toActionView(context, customHelpLink, packageID);
|
||||
|
||||
// Grant read permission to all the files in the package so embedded assets can be viewed
|
||||
ClipData clipData = new ClipData(null,
|
||||
new String[] {
|
||||
ClipDescription.MIMETYPE_TEXT_HTML,
|
||||
"text/css",
|
||||
"image/gif",
|
||||
"image/jpeg",
|
||||
"image/png"
|
||||
}, new ClipData.Item(contentUri));
|
||||
|
||||
// Exclude html help files and JS files. Treat rest of the files as assets
|
||||
FileFilter _fileFilter = new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
String name = pathname.getName();
|
||||
if (pathname.isFile() && (FileUtils.isReadmeFile(name) ||
|
||||
FileUtils.isWelcomeFile(name) || FileUtils.hasJavaScriptExtension(name))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
File packageDir = new File(
|
||||
context.getDir("data", Context.MODE_PRIVATE), "packages" + File.separator + packageID + File.separator);
|
||||
File[] files = packageDir.listFiles(_fileFilter);
|
||||
for(File assetFile : files) {
|
||||
Uri assetUri = FileProvider.getUriForFile(
|
||||
context, authority, assetFile);
|
||||
clipData.addItem(new ClipData.Item(assetUri));
|
||||
}
|
||||
|
||||
// Associate assets in clipData to the intent
|
||||
i.setClipData(clipData);
|
||||
} catch (NullPointerException e) {
|
||||
String message = "FileProvider undefined in app to load" + customHelp.toString();
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
|
||||
Log.e("KeyboardInfoActivity", message);
|
||||
}
|
||||
}
|
||||
else {
|
||||
i.setData(Uri.parse(customHelpLink));
|
||||
}
|
||||
if (FileProviderUtils.exists(context)|| KMManager.isTestMode()) {
|
||||
startActivity(i);
|
||||
}
|
||||
} else {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse(helpUrlStr));
|
||||
startActivity(i);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import android.widget.Toast;
|
|||
|
||||
import com.tavultesoft.kmea.util.FileUtils;
|
||||
import com.tavultesoft.kmea.util.FileProviderUtils;
|
||||
import com.tavultesoft.kmea.util.HelpFile;
|
||||
import com.tavultesoft.kmea.util.MapCompat;
|
||||
import com.tavultesoft.kmea.util.QRCodeUtil;
|
||||
|
||||
|
|
@ -140,33 +141,19 @@ public final class KeyboardSettingsActivity extends AppCompatActivity {
|
|||
|
||||
// "Help" link clicked
|
||||
if (itemTitle.equals(getString(R.string.help_link))) {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
||||
|
||||
if (customHelpLink != null) {
|
||||
if (FileUtils.isWelcomeFile(customHelpLink)) {
|
||||
File customHelp = new File(new File(customHelpLink).getAbsolutePath());
|
||||
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
// Starting with Android N, you can't pass file:// to intents, so we use FileProvider
|
||||
try {
|
||||
Uri contentUri = FileProvider.getUriForFile(
|
||||
context, authority, customHelp);
|
||||
i.setDataAndType(contentUri, "text/html");
|
||||
} catch (NullPointerException e) {
|
||||
String message = "FileProvider undefined in app to load" + customHelp.toString();
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
|
||||
Log.e("TAG", message);
|
||||
}
|
||||
}
|
||||
else {
|
||||
i.setData(Uri.parse(customHelpLink));
|
||||
}
|
||||
if (FileProviderUtils.exists(context)) {
|
||||
// Display local welcome.htm help file, including associated assets
|
||||
Intent i = HelpFile.toActionView(context, customHelpLink, packageID);
|
||||
|
||||
if (FileProviderUtils.exists(context) || KMManager.isTestMode()) {
|
||||
startActivity(i);
|
||||
}
|
||||
} else {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse(helpUrlStr));
|
||||
startActivity(i);
|
||||
}
|
||||
|
||||
// "Uninstall Keyboard" clicked
|
||||
} else if (itemTitle.equals(getString(R.string.uninstall_keyboard))) {
|
||||
// Uninstall selected keyboard
|
||||
|
|
|
|||
|
|
@ -0,0 +1,89 @@
|
|||
package com.tavultesoft.kmea.util;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipDescription;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
import com.tavultesoft.kmea.KMManager;
|
||||
import com.tavultesoft.kmea.util.FileProviderUtils;
|
||||
import com.tavultesoft.kmea.util.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
|
||||
|
||||
public final class HelpFile {
|
||||
private static final String TAG = "HelpFile";
|
||||
private static final String[] ASSET_MIME_TYPES = {
|
||||
ClipDescription.MIMETYPE_TEXT_HTML,
|
||||
"text/css",
|
||||
"image/gif",
|
||||
"image/jpeg",
|
||||
"image/png"};
|
||||
|
||||
/**
|
||||
* Utility to pass a help file and all associated assets to an Intent for Intent.ACTION_VIEW
|
||||
* @param context
|
||||
* @param helpFile Full path string of the html file to view
|
||||
* @param packageID String of the package ID
|
||||
* @return Intent
|
||||
*/
|
||||
public static Intent toActionView(Context context, String helpFile, String packageID) {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
||||
|
||||
if (FileUtils.isWelcomeFile(helpFile) && ! KMManager.isTestMode()) {
|
||||
File customHelp = new File(new File(helpFile).getAbsolutePath());
|
||||
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
// Starting with Android N, you can't pass file:// to intents, so we use FileProvider
|
||||
try {
|
||||
final String authority = FileProviderUtils.getAuthority(context);
|
||||
Uri contentUri = FileProvider.getUriForFile(
|
||||
context, authority, customHelp);
|
||||
i.setDataAndType(contentUri, "text/html");
|
||||
|
||||
// Grant read permission to all the files in the package so embedded assets can be viewed
|
||||
ClipData clipData = new ClipData(null, ASSET_MIME_TYPES, new ClipData.Item(contentUri));
|
||||
|
||||
// Exclude html help files and JS files. Treat rest of the files as assets
|
||||
FileFilter _fileFilter = new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
String name = pathname.getName();
|
||||
if (pathname.isFile() && (FileUtils.isReadmeFile(name) ||
|
||||
FileUtils.isWelcomeFile(name) || FileUtils.hasJavaScriptExtension(name))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
String base = helpFile.contains("packages") ? "packages" : "models";
|
||||
File packageDir = new File(
|
||||
context.getDir("data", Context.MODE_PRIVATE), base + File.separator + packageID + File.separator);
|
||||
File[] files = packageDir.listFiles(_fileFilter);
|
||||
for(File assetFile : files) {
|
||||
Uri assetUri = FileProvider.getUriForFile(
|
||||
context, authority, assetFile);
|
||||
clipData.addItem(new ClipData.Item(assetUri));
|
||||
}
|
||||
|
||||
// Associate assets in clipData to the intent
|
||||
i.setClipData(clipData);
|
||||
} catch (NullPointerException e) {
|
||||
String message = "FileProvider undefined in app to load" + customHelp.toString();
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
|
||||
Log.e(TAG, message);
|
||||
}
|
||||
} else {
|
||||
i.setData(Uri.parse(helpFile));
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue