Still can't open URI's for ZIP files on external storage, but we're close...

This commit is contained in:
d8ahazard 2016-09-15 07:35:12 -05:00
parent c5a73a5c19
commit 8f973661f4
3 changed files with 103 additions and 45 deletions

View File

@ -26,7 +26,8 @@ android {
} }
} }
repositories { repositories {
jcenter()
maven { url "https://jitpack.io" }
} }
dependencies { dependencies {
@ -35,6 +36,6 @@ dependencies {
compile 'com.android.support:cardview-v7:24.2.0' compile 'com.android.support:cardview-v7:24.2.0'
compile 'com.android.support:design:24.2.0' compile 'com.android.support:design:24.2.0'
compile 'com.jakewharton:butterknife:8.4.0' compile 'com.jakewharton:butterknife:8.4.0'
compile 'com.github.angads25:filepicker:1.0.6' compile 'com.github.michalis-vitos:aFileChooser:master'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
} }

View File

@ -5,8 +5,6 @@ import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout; import android.support.design.widget.TabLayout;
@ -14,7 +12,6 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -36,10 +33,9 @@ import butterknife.ButterKnife;
public class ModulesFragment extends Fragment { public class ModulesFragment extends Fragment {
private static final int FETCH_ZIP_CODE = 2;
public static List<Module> listModules = new ArrayList<>(); public static List<Module> listModules = new ArrayList<>();
public static List<Module> listModulesCache = new ArrayList<>(); public static List<Module> listModulesCache = new ArrayList<>();
private int viewPagePosition;
@BindView(R.id.progressBar) @BindView(R.id.progressBar)
ProgressBar progressBar; ProgressBar progressBar;
@BindView(R.id.fab) @BindView(R.id.fab)
@ -48,8 +44,8 @@ public class ModulesFragment extends Fragment {
ViewPager viewPager; ViewPager viewPager;
@BindView(R.id.tab_layout) @BindView(R.id.tab_layout)
TabLayout tabLayout; TabLayout tabLayout;
private int viewPagePosition;
private RepoHelper.TaskDelegate mTaskDelegate; private RepoHelper.TaskDelegate mTaskDelegate;
private static final int RQS_OPEN_DOCUMENT_TREE = 2;
@Nullable @Nullable
@Override @Override
@ -58,17 +54,16 @@ public class ModulesFragment extends Fragment {
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
fabio.setOnClickListener(v -> { fabio.setOnClickListener(v -> {
Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT); Intent fileIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
fileintent.setType("application/zip"); fileIntent.setType("application/zip");
startActivityForResult(fileintent, RQS_OPEN_DOCUMENT_TREE); fileIntent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(fileIntent, FETCH_ZIP_CODE);
}); });
new Utils.LoadModules(getActivity(), false).execute(); new Utils.LoadModules(getActivity(), false).execute();
mTaskDelegate = result -> { mTaskDelegate = result -> {
if (result.equals("OK")) { if (result.equals("OK")) {
Log.d("Magisk", "ModulesFragment: We dun got the result, hur hur.");
RefreshUI(); RefreshUI();
} }
@ -78,31 +73,14 @@ public class ModulesFragment extends Fragment {
return view; return view;
} }
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
String file = ""; Uri mUri = data.getData();
if (resultCode == Activity.RESULT_OK && requestCode == RQS_OPEN_DOCUMENT_TREE) { final int takeFlags = data.getFlags()
if (isExternalStorageDocument(data.getData())) { & (Intent.FLAG_GRANT_READ_URI_PERMISSION
final String docId = DocumentsContract.getDocumentId(data.getData()); | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
final String[] split = docId.split(":"); if (resultCode == Activity.RESULT_OK && requestCode == FETCH_ZIP_CODE) {
final String type = split[0]; new Utils.FlashZIP(getActivity(), mUri, takeFlags).execute();
if ("primary".equalsIgnoreCase(type)) {
file = Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
String shit = data.getDataString();
Log.d("Magisk", "ModulesFragment: Got a result, " + shit + " and " + data.getData().getAuthority() + " and " + file);
} }
} }
@ -111,10 +89,8 @@ public class ModulesFragment extends Fragment {
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_module, menu); inflater.inflate(R.menu.menu_module, menu);
} }
private void RefreshUI() { private void RefreshUI() {
viewPagePosition = tabLayout.getSelectedTabPosition(); viewPagePosition = tabLayout.getSelectedTabPosition();
listModules.clear(); listModules.clear();
@ -129,7 +105,6 @@ public class ModulesFragment extends Fragment {
new updateUI().execute(); new updateUI().execute();
} }
void selectPage(int pageIndex) { void selectPage(int pageIndex) {
tabLayout.setScrollPosition(pageIndex, 0f, true); tabLayout.setScrollPosition(pageIndex, 0f, true);
viewPager.setCurrentItem(pageIndex); viewPager.setCurrentItem(pageIndex);
@ -153,7 +128,6 @@ public class ModulesFragment extends Fragment {
} }
private class updateUI extends AsyncTask<Void, Void, Void> { private class updateUI extends AsyncTask<Void, Void, Void> {
@Override @Override

View File

@ -5,6 +5,7 @@ import android.app.Activity;
import android.app.DownloadManager; import android.app.DownloadManager;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
@ -13,6 +14,7 @@ import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Environment; import android.os.Environment;
import android.provider.DocumentsContract;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
@ -33,16 +35,18 @@ import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException; import java.security.spec.InvalidKeySpecException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
@ -483,30 +487,106 @@ public class Utils {
return null; return null;
} }
} }
public static class FlashZIP extends AsyncTask<Void, Void, Boolean> { public static class FlashZIP extends AsyncTask<Void, Void, Boolean> {
private String mPath, mName; private String mPath, mName;
private Uri mUri;
private ProgressDialog progress; private ProgressDialog progress;
private File mFile;
private Context mContext; private Context mContext;
private List<String> ret; private List<String> ret;
private boolean deleteFileAfter;
public FlashZIP(Context context, String name, String path) { public FlashZIP(Context context, String name, String path) {
mContext = context; mContext = context;
mName = name; mName = name;
mPath = path; mPath = path;
deleteFileAfter = false;
}
public FlashZIP(Context context, Uri uRi, int flags) {
mContext = context;
mUri = uRi;
deleteFileAfter = true;
String file = "";
final String docId = DocumentsContract.getDocumentId(mUri);
Log.d("Magisk","Utils: FlashZip Running, " + docId + " and " + mUri.toString());
String[] split = docId.split(":");
mName = split[1];
if (mName.contains("/")) {
split = mName.split("/");
}
if (split[1].contains(".zip")) {
file = mContext.getFilesDir() + "/" + split[1];
Log.d("Magisk", "Utils: FlashZip running for uRI " + mUri.toString());
} else {
Log.e("Magisk", "Utils: error parsing Zipfile " + mUri.getPath());
this.cancel(true);
}
ContentResolver contentResolver = mContext.getContentResolver();
contentResolver.takePersistableUriPermission(mUri, flags);
try {
InputStream in = contentResolver.openInputStream(mUri);
Log.d("Magisk", "Firing inputStream");
mFile = createFileFromInputStream(in, file, mContext);
if (mFile != null) {
mPath = mFile.getPath();
Log.d("Magisk", "Utils: Mpath is " + mPath);
} else {
Log.e("Magisk", "Utils: error creating file " + mUri.getPath());
this.cancel(true);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// TODO handle non-primary volumes
}
private static File createFileFromInputStream(InputStream inputStream, String fileName, Context context) {
try {
File f = new File(fileName);
f.setWritable(true, false);
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputStream.close();
Log.d("Magisk", "Holy balls, I think it worked. File is " + f.getPath());
return f;
} catch (IOException e) {
System.out.println("error in creating a file");
e.printStackTrace();
}
return null;
} }
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
super.onPreExecute(); super.onPreExecute();
progress = ProgressDialog.show(mContext, mContext.getString(R.string.zip_install_progress_title), mContext.getString(R.string.zip_install_progress_msg, mName)); progress = ProgressDialog.show(mContext, mContext.getString(R.string.zip_install_progress_title), mContext.getString(R.string.zip_install_progress_msg, mName));
} }
@Override @Override
protected Boolean doInBackground(Void... voids) { protected Boolean doInBackground(Void... voids) {
if (mPath != null) {
Log.e("Magisk", "Utils: Error, flashZIP called without a valid zip file to flash.");
this.cancel(true);
return false;
}
if (!Shell.rootAccess()) { if (!Shell.rootAccess()) {
return false; return false;
} else { } else {
@ -518,7 +598,7 @@ public class Utils {
"BOOTMODE=true sh /data/tmp/META-INF/com/google/android/update-binary dummy 1 /data/tmp/install.zip", "BOOTMODE=true sh /data/tmp/META-INF/com/google/android/update-binary dummy 1 /data/tmp/install.zip",
"if [ $? -eq 0 ]; then echo true; else echo false; fi" "if [ $? -eq 0 ]; then echo true; else echo false; fi"
); );
return Boolean.parseBoolean(ret.get(ret.size() - 1)); return ret != null && Boolean.parseBoolean(ret.get(ret.size() - 1));
} }
} }
@ -526,6 +606,10 @@ public class Utils {
protected void onPostExecute(Boolean result) { protected void onPostExecute(Boolean result) {
super.onPostExecute(result); super.onPostExecute(result);
Shell.su("rm -rf /data/tmp"); Shell.su("rm -rf /data/tmp");
if (deleteFileAfter) {
Shell.su("rm -rf " + mPath);
Log.d("Magisk", "Utils: Deleting file " + mPath);
}
progress.dismiss(); progress.dismiss();
if (!result) { if (!result) {
Toast.makeText(mContext, mContext.getString(R.string.manual_install, mPath), Toast.LENGTH_LONG).show(); Toast.makeText(mContext, mContext.getString(R.string.manual_install, mPath), Toast.LENGTH_LONG).show();
@ -550,5 +634,4 @@ public class Utils {
} }
} }