Work on file picker - WIP

This commit is contained in:
d8ahazard 2016-09-14 17:12:47 -05:00
parent 46abbfe224
commit 6a90340b14
5 changed files with 69 additions and 54 deletions

View File

@ -26,9 +26,7 @@ android {
}
}
repositories {
maven {
url "http://dl.bintray.com/lukaville/maven"
}
}
dependencies {
@ -36,7 +34,7 @@ dependencies {
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.android.support:cardview-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile 'com.nbsp:library:1.1'
compile 'com.jakewharton:butterknife:8.4.0'
compile 'com.github.angads25:filepicker:1.0.6'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

View File

@ -16,6 +16,7 @@
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity
android:name=".WelcomeActivity"
android:exported="true"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@ -23,9 +24,11 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".AboutActivity"
android:theme="@style/AppTheme.Transparent"/>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.topjohnwu.magisk.provider"
@ -35,6 +38,7 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>

View File

@ -167,9 +167,6 @@ public abstract class BaseModuleFragment extends Fragment {
String logUrl = module.getmLogUrl();
String supportUrl = module.getmSupportUrl();
String donateUrl = module.getmDonateUrl();
if (supportUrl != null && !supportUrl.isEmpty()) holder.supportLink.setBackgroundColor(Color.GRAY);
if (logUrl != null && !logUrl.isEmpty()) holder.changeLog.setBackgroundColor(Color.GRAY);
if (donateUrl != null && !donateUrl.isEmpty()) holder.authorLink.setBackgroundColor(Color.GRAY);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.contains("ignoreUpdateAlerts")) {
ignoreAlertUpdate = prefs.getBoolean("ignoreUpdateAlerts", false);

View File

@ -2,40 +2,46 @@ package com.topjohnwu.magisk;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.provider.DocumentFile;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.nbsp.materialfilepicker.MaterialFilePicker;
import com.nbsp.materialfilepicker.ui.FilePickerActivity;
import com.github.angads25.filepicker.controller.DialogSelectionListener;
import com.github.angads25.filepicker.model.DialogConfigs;
import com.github.angads25.filepicker.model.DialogProperties;
import com.github.angads25.filepicker.view.FilePickerDialog;
import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.module.Repo;
import com.topjohnwu.magisk.module.RepoHelper;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.lib;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
import butterknife.BindView;
import butterknife.ButterKnife;
@ -44,9 +50,7 @@ public class ModulesFragment extends Fragment {
public static List<Module> listModules = new ArrayList<>();
public static List<Module> listModulesCache = new ArrayList<>();
private static final int FILE_SELECT_CODE = 0;
private int viewPagePosition;
private static final int RESULT_OK = 1;
@BindView(R.id.progressBar)
ProgressBar progressBar;
@ -57,6 +61,7 @@ public class ModulesFragment extends Fragment {
@BindView(R.id.tab_layout)
TabLayout tabLayout;
private RepoHelper.TaskDelegate mTaskDelegate;
private static final int RQS_OPEN_DOCUMENT_TREE = 2;
@Nullable
@Override
@ -64,61 +69,67 @@ public class ModulesFragment extends Fragment {
View view = inflater.inflate(R.layout.modules_fragment, container, false);
ButterKnife.bind(this, view);
String[] extensions = new String[1];
extensions[0] = "zip";
fabio.setOnClickListener(v -> {
Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
fileintent.setType("application/zip");
startActivityForResult(fileintent, RQS_OPEN_DOCUMENT_TREE);
});
new Utils.LoadModules(getActivity(), false).execute();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mTaskDelegate = result -> {
if (result.equals("OK")) {
Log.d("Magisk","ModulesFragment: We dun got the result, hur hur.");
Log.d("Magisk", "ModulesFragment: We dun got the result, hur hur.");
RefreshUI();
}
};
new updateUI().execute();
return view;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String file = "";
if(resultCode == Activity.RESULT_OK && requestCode == RQS_OPEN_DOCUMENT_TREE){
if (isExternalStorageDocument(data.getData())) {
final String docId = DocumentsContract.getDocumentId(data.getData());
final String[] split = docId.split(":");
final String type = split[0];
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);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_module, menu);
fabio.setOnClickListener(view -> {
openFilePicker();
});
}
private void openFilePicker() {
new MaterialFilePicker()
.withSupportFragment(this)
.withFilter(Pattern.compile(".*\\.zip$"))
.withRequestCode(FILE_SELECT_CODE)
.withHiddenFiles(true)
.start();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("Magisk","WelcomeActivity: Got an OK result" + resultCode);
if (resultCode == Activity.RESULT_OK) {
String path = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
Log.d("Magisk","ModuleFragment: Got an OK result " + path);
if (path != null) {
Log.d("Path: ", path);
Toast.makeText(getActivity(), "Picked file: " + path, Toast.LENGTH_LONG).show();
// Get the Uri of the selected file
String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
Uri uri = Uri.parse(filePath);
path = uri.getPath();
Log.d("Magisk","ModuleFragment: Got an OK result " + filePath + " and " + uri.toString() + " and " + path);
String fileName = uri.getLastPathSegment();
new Utils.FlashZIP(getActivity(), fileName, path).execute();
}
}
}
private void RefreshUI() {
viewPagePosition = tabLayout.getSelectedTabPosition();
@ -129,8 +140,8 @@ public class ModulesFragment extends Fragment {
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(viewPagePosition);
new Utils.LoadModules(getActivity(), true).execute();
Collections.sort(listModules,new CustomComparator());
Collections.sort(listModulesCache,new CustomComparator());
Collections.sort(listModules, new CustomComparator());
Collections.sort(listModulesCache, new CustomComparator());
new updateUI().execute();
}
@ -210,6 +221,7 @@ public class ModulesFragment extends Fragment {
}
}
}
public class CustomComparator implements Comparator<Module> {
@Override
public int compare(Module o1, Module o2) {

View File

@ -18,6 +18,7 @@ import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
@ -43,6 +44,9 @@ public class RepoHelper {
if (!prefs.contains("hasCachedRepos") | refresh) {
Log.d(TAG, "RepoHelper: Building from web");
new BuildFromWeb(delegate).execute();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
String date = format.format(Calendar.getInstance().getTime());
prefs.edit().putString("last_update",date).apply();
} else {
Log.d(TAG, "RepoHelper: Building from cache");
BuildFromCache();