rename package to app.testfileprovider
This commit is contained in:
parent
d5afbdb037
commit
c588df413b
7 changed files with 178 additions and 368 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="app.installapks"
|
package="app.testfileprovider"
|
||||||
android:versionCode="1"
|
android:versionCode="1"
|
||||||
android:versionName="1.0">
|
android:versionName="1.0">
|
||||||
<uses-sdk android:minSdkVersion="30"
|
<uses-sdk android:minSdkVersion="30"
|
||||||
|
|
@ -12,29 +12,17 @@
|
||||||
<uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED"/>
|
<uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED"/>
|
||||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
||||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
|
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
|
||||||
<application android:debuggable="true" android:label="install APKs" android:icon="@drawable/appicon">
|
<application android:debuggable="true" android:label="FileProvider" android:icon="@drawable/appicon">
|
||||||
<provider
|
<provider
|
||||||
android:name="app.installapks.FileProvider"
|
android:name="app.testfileprovider.FileProvider"
|
||||||
android:authorities="app.installapks"
|
android:authorities="app.testfileprovider"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
/>
|
/>
|
||||||
<!-- this below is commented out becuase it stupidly requires the use of JETPACK fuckery -->
|
<activity android:name="app.testfileprovider.AppActivity"
|
||||||
<!--
|
|
||||||
<provider
|
|
||||||
android:name="app.installapks.Locandroidx.core.content.FileProvider"
|
|
||||||
android:authorities="com.example.myapp.fileprovider"
|
|
||||||
android:grantUriPermissions="true"
|
|
||||||
android:exported="false">
|
|
||||||
<meta-data
|
|
||||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
|
||||||
android:resource="@xml/filepaths" />
|
|
||||||
</provider>
|
|
||||||
-->
|
|
||||||
<activity android:name="app.installapks.AppActivity"
|
|
||||||
|
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:configChanges="orientation|screenSize|keyboardHidden"
|
android:configChanges="orientation|screenSize|keyboardHidden"
|
||||||
android:label="install APKs">
|
android:label="FileProvider">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
|
|
||||||
|
|
@ -1,181 +0,0 @@
|
||||||
package app.installapks;
|
|
||||||
import android.provider.Settings ;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.os.Environment;
|
|
||||||
//import android.view.MenuItem;
|
|
||||||
import android.view.*;
|
|
||||||
// for WebView,WebMessage,WebMessagePort,
|
|
||||||
import android.webkit.*;
|
|
||||||
import android.net.Uri;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
|
|
||||||
public class AppActivity extends Activity {
|
|
||||||
|
|
||||||
private static final String BASE_URI = "https://alexmahr.de";
|
|
||||||
|
|
||||||
// private void installApkProgramatically() {
|
|
||||||
// try {
|
|
||||||
// File path = activity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
|
|
||||||
//
|
|
||||||
// File file = new File(path, filename);
|
|
||||||
//
|
|
||||||
// Uri uri;
|
|
||||||
//
|
|
||||||
// if (file.exists()) {
|
|
||||||
//
|
|
||||||
// Intent unKnownSourceIntent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", activity.getPackageName())));
|
|
||||||
//
|
|
||||||
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
//
|
|
||||||
// if (!activity.getPackageManager().canRequestPackageInstalls()) {
|
|
||||||
// startActivityForResult(unKnownSourceIntent, Constant.UNKNOWN_RESOURCE_INTENT_REQUEST_CODE);
|
|
||||||
// } else {
|
|
||||||
// Uri fileUri = FileProvider.getUriForFile(activity.getBaseContext(), activity.getApplicationContext().getPackageName() + ".provider", file);
|
|
||||||
// Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
|
|
||||||
// intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
|
|
||||||
// intent.setDataAndType(fileUri, "application/vnd.android" + ".package-archive");
|
|
||||||
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
||||||
// intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
||||||
// startActivity(intent);
|
|
||||||
// alertDialog.dismiss();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
//
|
|
||||||
// Intent intent1 = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
|
||||||
// uri = FileProvider.getUriForFile(activity.getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", file);
|
|
||||||
// activity.grantUriPermission("com.abcd.xyz", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
||||||
// activity.grantUriPermission("com.abcd.xyz", uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
|
||||||
// intent1.setDataAndType(uri,
|
|
||||||
// "application/*");
|
|
||||||
// intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
||||||
// intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
||||||
// intent1.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
|
||||||
// startActivity(intent1);
|
|
||||||
//
|
|
||||||
// } else {
|
|
||||||
// Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
||||||
//
|
|
||||||
// uri = Uri.fromFile(file);
|
|
||||||
//
|
|
||||||
// intent.setDataAndType(uri,
|
|
||||||
// "application/vnd.android.package-archive");
|
|
||||||
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
||||||
// startActivity(intent);
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
//
|
|
||||||
// Log.i(TAG, " file " + file.getPath() + " does not exist");
|
|
||||||
// }
|
|
||||||
// } catch (Exception e) {
|
|
||||||
//
|
|
||||||
// Log.i(TAG, "" + e.getMessage());
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
public String readFileFromAssets(String filename) {
|
|
||||||
String filecontents = "";
|
|
||||||
try {
|
|
||||||
InputStream stream = getAssets().open(filename);
|
|
||||||
int filesize = stream.available();
|
|
||||||
byte[] filebuffer = new byte[filesize];
|
|
||||||
stream.read(filebuffer);
|
|
||||||
stream.close();
|
|
||||||
filecontents = new String(filebuffer);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// I <3 java exceptions
|
|
||||||
}
|
|
||||||
return filecontents;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
// this removes the title bar (a ~1cm big strip at the top of the app showing its name
|
|
||||||
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
||||||
// we create the webview (at least on the android 14 that is a webview that features avif + websockets etc....)
|
|
||||||
WebView myWebView = new WebView(this);//activityContext);
|
|
||||||
// MyJavascriptInterface myJavaScriptInterface = new MyJavascriptInterface(this,myWebView);
|
|
||||||
// we create a webview (there is also setwebviewclient-vs-setwebchromeclient
|
|
||||||
WebViewClient myWebViewClient= new WebViewClient();
|
|
||||||
myWebView.setWebViewClient(myWebViewClient);
|
|
||||||
// to setup settings
|
|
||||||
WebSettings myWebSettings = myWebView.getSettings();
|
|
||||||
myWebSettings.setBuiltInZoomControls(true);
|
|
||||||
myWebSettings.setDisplayZoomControls(false);
|
|
||||||
myWebSettings.setJavaScriptEnabled(true);
|
|
||||||
myWebSettings.setDomStorageEnabled(true);
|
|
||||||
myWebSettings.setDatabaseEnabled(true);
|
|
||||||
myWebSettings.setDatabasePath("/data/data/" + myWebView.getContext().getPackageName() + "/databases/");
|
|
||||||
myWebView.addJavascriptInterface(this, "myJavaScriptInterface");
|
|
||||||
// load the html from assets file
|
|
||||||
String html = readFileFromAssets("index.html");
|
|
||||||
myWebView.loadDataWithBaseURL(BASE_URI,html, "text/html", "UTF-8",null);
|
|
||||||
//myWebView.loadData(encodedHtml, "text/html", "base64");
|
|
||||||
// alternatively this could be to load a website
|
|
||||||
//myWebView.loadUrl("https://alexmahr.de/ru");
|
|
||||||
setContentView(myWebView);
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Override
|
|
||||||
// public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
||||||
// super.onActivityResult(requestCode, resultCode, data);
|
|
||||||
// switch (requestCode) {
|
|
||||||
//
|
|
||||||
// case Constant.UNKNOWN_RESOURCE_INTENT_REQUEST_CODE:
|
|
||||||
// switch (resultCode) {
|
|
||||||
// case Activity.RESULT_OK:
|
|
||||||
// installApkProgramatically();
|
|
||||||
//
|
|
||||||
// break;
|
|
||||||
// case Activity.RESULT_CANCELED:
|
|
||||||
// //unknown resouce installation cancelled
|
|
||||||
//
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
|
|
||||||
@JavascriptInterface
|
|
||||||
public String startActivity(String fileName) {
|
|
||||||
File filesDir = getFilesDir();
|
|
||||||
Log.i("startActivity with Uri",FileProvider.CONTENT_URI
|
|
||||||
+ fileName);
|
|
||||||
Intent i = new Intent(Intent.ACTION_VIEW,
|
|
||||||
Uri.parse(FileProvider.CONTENT_URI
|
|
||||||
+ fileName));
|
|
||||||
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
||||||
startActivity(i);
|
|
||||||
return filesDir.getAbsolutePath();
|
|
||||||
}
|
|
||||||
|
|
||||||
@JavascriptInterface
|
|
||||||
public String download(String downloadurl) {
|
|
||||||
File filesDir = getFilesDir();
|
|
||||||
Log.i("BAAAAA",FileProvider.CONTENT_URI
|
|
||||||
+ "foto.jpg");
|
|
||||||
Intent i = new Intent(Intent.ACTION_VIEW,
|
|
||||||
Uri.parse(FileProvider.CONTENT_URI
|
|
||||||
+ "foto.jpg"));
|
|
||||||
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
||||||
startActivity(i);
|
|
||||||
|
|
||||||
return filesDir.getAbsolutePath();
|
|
||||||
// this.webview.evaluateJavascript("(setTimeout(()=>{document.body.innerHTML='all gone';},2000)()",null);
|
|
||||||
// return "this is good";
|
|
||||||
}
|
|
||||||
|
|
||||||
@JavascriptInterface
|
|
||||||
public String toString() {
|
|
||||||
// this.webview.evaluateJavascript("(setTimeout(()=>{document.body.innerHTML='all gone';},2000)()",null);
|
|
||||||
return "this is good";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,117 +0,0 @@
|
||||||
/***
|
|
||||||
Copyright (c) 2008-2014 CommonsWare, LLC
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
||||||
use this file except in compliance with the License. You may obtain a copy
|
|
||||||
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
|
|
||||||
by applicable law or agreed to in writing, software distributed under the
|
|
||||||
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
|
|
||||||
OF ANY KIND, either express or implied. See the License for the specific
|
|
||||||
language governing permissions and limitations under the License.
|
|
||||||
|
|
||||||
Covered in detail in the book _The Busy Coder's Guide to Android Development_
|
|
||||||
https://commonsware.com/Android
|
|
||||||
*/
|
|
||||||
|
|
||||||
package app.installapks;
|
|
||||||
|
|
||||||
import android.content.res.AssetManager;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.ParcelFileDescriptor;
|
|
||||||
import android.util.Log;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class FileProvider extends AbstractFileProvider {
|
|
||||||
public static final Uri CONTENT_URI=
|
|
||||||
Uri.parse("content://app.installapks/");
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onCreate() {
|
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public File openAssetFile(String path)
|
|
||||||
throws FileNotFoundException {
|
|
||||||
File root=getContext().getFilesDir();
|
|
||||||
File f = new File(root, path).getAbsoluteFile();
|
|
||||||
if(!f.exists()){
|
|
||||||
AssetManager assets=getContext().getAssets();
|
|
||||||
try {
|
|
||||||
copy(assets.open(path.substring(1)), f);
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
Log.e("FileProvider", "Exception copying from assets", e);
|
|
||||||
throw new FileNotFoundException(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return f;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ParcelFileDescriptor openFile(Uri uri, String mode)
|
|
||||||
throws FileNotFoundException {
|
|
||||||
File root=getContext().getFilesDir();
|
|
||||||
File f = openAssetFile(uri.getPath());
|
|
||||||
|
|
||||||
if (!f.getPath().startsWith(root.getPath())) {
|
|
||||||
throw new
|
|
||||||
SecurityException("Resolved path jumped beyond root");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (f.exists()) {
|
|
||||||
return(ParcelFileDescriptor.open(f, parseMode(mode)));
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new FileNotFoundException(uri.getPath());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected long getDataLength(Uri uri)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
File f = openAssetFile(uri.getPath());
|
|
||||||
return(f.length());
|
|
||||||
} catch (Exception e) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// following is from ParcelFileDescriptor source code
|
|
||||||
// Copyright (C) 2006 The Android Open Source Project
|
|
||||||
// (even though this method was added much after 2006...)
|
|
||||||
|
|
||||||
private static int parseMode(String mode) {
|
|
||||||
final int modeBits;
|
|
||||||
if ("r".equals(mode)) {
|
|
||||||
modeBits=ParcelFileDescriptor.MODE_READ_ONLY;
|
|
||||||
}
|
|
||||||
else if ("w".equals(mode) || "wt".equals(mode)) {
|
|
||||||
modeBits=
|
|
||||||
ParcelFileDescriptor.MODE_WRITE_ONLY
|
|
||||||
| ParcelFileDescriptor.MODE_CREATE
|
|
||||||
| ParcelFileDescriptor.MODE_TRUNCATE;
|
|
||||||
}
|
|
||||||
else if ("wa".equals(mode)) {
|
|
||||||
modeBits=
|
|
||||||
ParcelFileDescriptor.MODE_WRITE_ONLY
|
|
||||||
| ParcelFileDescriptor.MODE_CREATE
|
|
||||||
| ParcelFileDescriptor.MODE_APPEND;
|
|
||||||
}
|
|
||||||
else if ("rw".equals(mode)) {
|
|
||||||
modeBits=
|
|
||||||
ParcelFileDescriptor.MODE_READ_WRITE
|
|
||||||
| ParcelFileDescriptor.MODE_CREATE;
|
|
||||||
}
|
|
||||||
else if ("rwt".equals(mode)) {
|
|
||||||
modeBits=
|
|
||||||
ParcelFileDescriptor.MODE_READ_WRITE
|
|
||||||
| ParcelFileDescriptor.MODE_CREATE
|
|
||||||
| ParcelFileDescriptor.MODE_TRUNCATE;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new IllegalArgumentException("Bad mode '" + mode + "'");
|
|
||||||
}
|
|
||||||
return modeBits;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
/* AUTO-GENERATED FILE. DO NOT MODIFY.
|
|
||||||
*
|
|
||||||
* This class was automatically generated by the
|
|
||||||
* aapt tool from the resource data it found. It
|
|
||||||
* should not be modified by hand.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package app.installapks;
|
|
||||||
|
|
||||||
public final class R {
|
|
||||||
public static final class attr {
|
|
||||||
}
|
|
||||||
public static final class drawable {
|
|
||||||
public static final int appicon=0x7f020000;
|
|
||||||
}
|
|
||||||
public static final class xml {
|
|
||||||
public static final int filepaths=0x7f030000;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
86
app/src/app/testfileprovider/AppActivity.java
Normal file
86
app/src/app/testfileprovider/AppActivity.java
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
package app.testfileprovider;
|
||||||
|
import android.provider.Settings ;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Environment;
|
||||||
|
//import android.view.MenuItem;
|
||||||
|
import android.view.*;
|
||||||
|
// for WebView,WebMessage,WebMessagePort,
|
||||||
|
import android.webkit.*;
|
||||||
|
import android.net.Uri;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
|
||||||
|
public class AppActivity extends Activity {
|
||||||
|
|
||||||
|
private static final String BASE_URI = "https://alexmahr.de";
|
||||||
|
|
||||||
|
public String readFileFromAssets(String filename) {
|
||||||
|
String filecontents = "";
|
||||||
|
try {
|
||||||
|
InputStream stream = getAssets().open(filename);
|
||||||
|
int filesize = stream.available();
|
||||||
|
byte[] filebuffer = new byte[filesize];
|
||||||
|
stream.read(filebuffer);
|
||||||
|
stream.close();
|
||||||
|
filecontents = new String(filebuffer);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// I <3 java exceptions
|
||||||
|
}
|
||||||
|
return filecontents;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
// this removes the title bar (a ~1cm big strip at the top of the app showing its name
|
||||||
|
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
|
// we create the webview (at least on the android 14 that is a webview that features avif + websockets etc....)
|
||||||
|
WebView myWebView = new WebView(this);//activityContext);
|
||||||
|
// MyJavascriptInterface myJavaScriptInterface = new MyJavascriptInterface(this,myWebView);
|
||||||
|
// we create a webview (there is also setwebviewclient-vs-setwebchromeclient
|
||||||
|
WebViewClient myWebViewClient= new WebViewClient();
|
||||||
|
myWebView.setWebViewClient(myWebViewClient);
|
||||||
|
// to setup settings
|
||||||
|
WebSettings myWebSettings = myWebView.getSettings();
|
||||||
|
myWebSettings.setBuiltInZoomControls(true);
|
||||||
|
myWebSettings.setDisplayZoomControls(false);
|
||||||
|
myWebSettings.setJavaScriptEnabled(true);
|
||||||
|
myWebSettings.setDomStorageEnabled(true);
|
||||||
|
myWebSettings.setDatabaseEnabled(true);
|
||||||
|
myWebSettings.setDatabasePath("/data/data/" + myWebView.getContext().getPackageName() + "/databases/");
|
||||||
|
myWebView.addJavascriptInterface(this, "myJavaScriptInterface");
|
||||||
|
// load the html from assets file
|
||||||
|
String html = readFileFromAssets("index.html");
|
||||||
|
myWebView.loadDataWithBaseURL(BASE_URI,html, "text/html", "UTF-8",null);
|
||||||
|
//myWebView.loadData(encodedHtml, "text/html", "base64");
|
||||||
|
// alternatively this could be to load a website
|
||||||
|
//myWebView.loadUrl("https://alexmahr.de/ru");
|
||||||
|
setContentView(myWebView);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@JavascriptInterface
|
||||||
|
public String startActivity(String fileName) {
|
||||||
|
File filesDir = getFilesDir();
|
||||||
|
Log.i("startActivity with Uri",FileProvider.CONTENT_URI
|
||||||
|
+ fileName);
|
||||||
|
Intent i = new Intent(Intent.ACTION_VIEW,
|
||||||
|
Uri.parse(FileProvider.CONTENT_URI
|
||||||
|
+ fileName));
|
||||||
|
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||||
|
startActivity(i);
|
||||||
|
return filesDir.getAbsolutePath();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@JavascriptInterface
|
||||||
|
public String toString() {
|
||||||
|
return "this is good";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/***
|
/***
|
||||||
Copyright (c) 2014-2015 CommonsWare, LLC
|
Copyright (c) 2008-2014 CommonsWare, LLC
|
||||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
use this file except in compliance with the License. You may obtain a copy
|
use this file except in compliance with the License. You may obtain a copy
|
||||||
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
|
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
|
||||||
|
|
@ -12,27 +12,109 @@
|
||||||
https://commonsware.com/Android
|
https://commonsware.com/Android
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package app.installapks;
|
package app.testfileprovider;
|
||||||
|
|
||||||
|
import android.content.res.AssetManager;
|
||||||
import android.content.ContentProvider;
|
import android.content.ContentProvider;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.res.AssetFileDescriptor;
|
import android.content.res.AssetFileDescriptor;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.database.MatrixCursor;
|
import android.database.MatrixCursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.ParcelFileDescriptor;
|
||||||
import android.provider.OpenableColumns;
|
import android.provider.OpenableColumns;
|
||||||
|
import android.util.Log;
|
||||||
// via local java file
|
// via local java file
|
||||||
// copied from https://github.com/MuntashirAkon/cwac-provider/blob/3dccc0ef9cd2eda5fe149b87ff4ef4881c583f8c/provider/src/main/java/com/commonsware/cwac/provider/LegacyCompatCursorWrapper.java
|
// copied from https://github.com/MuntashirAkon/cwac-provider/blob/3dccc0ef9cd2eda5fe149b87ff4ef4881c583f8c/provider/src/main/java/com/commonsware/cwac/provider/LegacyCompatCursorWrapper.java
|
||||||
//import com.commonsware.cwac.provider.LegacyCompatCursorWrapper;
|
//import com.commonsware.cwac.provider.LegacyCompatCursorWrapper;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
|
||||||
abstract class AbstractFileProvider extends ContentProvider {
|
public class FileProvider extends ContentProvider {
|
||||||
private final static String[] OPENABLE_PROJECTION= {
|
private final static String[] OPENABLE_PROJECTION= {
|
||||||
OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE };
|
OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE };
|
||||||
|
public static final Uri CONTENT_URI=
|
||||||
|
Uri.parse("content://app.testfileprovider/");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCreate() {
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public File openAssetFile(String path)
|
||||||
|
throws FileNotFoundException {
|
||||||
|
File root=getContext().getFilesDir();
|
||||||
|
File f = new File(root, path).getAbsoluteFile();
|
||||||
|
if(!f.exists()){
|
||||||
|
AssetManager assets=getContext().getAssets();
|
||||||
|
try {
|
||||||
|
copy(assets.open(path.substring(1)), f);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
Log.e("FileProvider", "Exception copying from assets", e);
|
||||||
|
throw new FileNotFoundException(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ParcelFileDescriptor openFile(Uri uri, String mode)
|
||||||
|
throws FileNotFoundException {
|
||||||
|
File root=getContext().getFilesDir();
|
||||||
|
File f = openAssetFile(uri.getPath());
|
||||||
|
|
||||||
|
if (!f.getPath().startsWith(root.getPath())) {
|
||||||
|
throw new
|
||||||
|
SecurityException("Resolved path jumped beyond root");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (f.exists()) {
|
||||||
|
return(ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY));
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new FileNotFoundException(uri.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected long getDataLength(Uri uri)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
File f = openAssetFile(uri.getPath());
|
||||||
|
return(f.length());
|
||||||
|
} catch (Exception e) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getFileName(Uri uri) {
|
||||||
|
return(uri.getLastPathSegment());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType(Uri uri) {
|
||||||
|
return(URLConnection.guessContentTypeFromName(uri.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Uri insert(Uri uri, ContentValues initialValues) {
|
||||||
|
throw new RuntimeException("Operation not supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int update(Uri uri, ContentValues values, String where,
|
||||||
|
String[] whereArgs) {
|
||||||
|
throw new RuntimeException("Operation not supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int delete(Uri uri, String where, String[] whereArgs) {
|
||||||
|
throw new RuntimeException("Operation not supported");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Cursor query(Uri uri, String[] projection, String selection,
|
public Cursor query(Uri uri, String[] projection, String selection,
|
||||||
|
|
@ -60,35 +142,6 @@ abstract class AbstractFileProvider extends ContentProvider {
|
||||||
return(new LegacyCompatCursorWrapper(cursor));
|
return(new LegacyCompatCursorWrapper(cursor));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getType(Uri uri) {
|
|
||||||
return(URLConnection.guessContentTypeFromName(uri.toString()));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected String getFileName(Uri uri) {
|
|
||||||
return(uri.getLastPathSegment());
|
|
||||||
}
|
|
||||||
|
|
||||||
protected long getDataLength(Uri uri) {
|
|
||||||
return(AssetFileDescriptor.UNKNOWN_LENGTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Uri insert(Uri uri, ContentValues initialValues) {
|
|
||||||
throw new RuntimeException("Operation not supported");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int update(Uri uri, ContentValues values, String where,
|
|
||||||
String[] whereArgs) {
|
|
||||||
throw new RuntimeException("Operation not supported");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int delete(Uri uri, String where, String[] whereArgs) {
|
|
||||||
throw new RuntimeException("Operation not supported");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void copy(InputStream in, File dst)
|
static void copy(InputStream in, File dst)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
FileOutputStream out=new FileOutputStream(dst);
|
FileOutputStream out=new FileOutputStream(dst);
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package app.installapks;
|
package app.testfileprovider;
|
||||||
|
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.database.CursorWrapper;
|
import android.database.CursorWrapper;
|
||||||
Loading…
Add table
Reference in a new issue