diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesAdapter.java b/app/src/main/java/com/topjohnwu/magisk/ModulesAdapter.java index f5cf729f1..ff25635ad 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ModulesAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/ModulesAdapter.java @@ -10,6 +10,7 @@ import android.widget.ImageView; import android.widget.TextView; import com.topjohnwu.magisk.module.Module; +import com.topjohnwu.magisk.module.ModuleRepo; import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java index a67b47c6e..4d2cbd1f9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java @@ -12,6 +12,7 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -21,14 +22,12 @@ import android.view.ViewGroup; import android.widget.ProgressBar; import com.topjohnwu.magisk.module.Module; -import com.topjohnwu.magisk.utils.Shell; +import com.topjohnwu.magisk.module.ModuleRepo; import com.topjohnwu.magisk.utils.Utils; import java.io.File; -import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.ExecutionException; import butterknife.BindView; import butterknife.ButterKnife; @@ -37,6 +36,7 @@ public class ModulesFragment extends Fragment { public static List listModules = new ArrayList<>(); public static List listModulesCache = new ArrayList<>(); + public static List listModulesDownload = new ArrayList<>(); private static final int FILE_SELECT_CODE = 0; private File input; @@ -73,6 +73,7 @@ public class ModulesFragment extends Fragment { }); + } public void onActivityResult(int requestCode, int resultCode, Intent data) { @@ -95,6 +96,7 @@ public class ModulesFragment extends Fragment { case R.id.force_reload: listModules.clear(); listModulesCache.clear(); + listModulesDownload.clear(); progressBar.setVisibility(View.VISIBLE); viewPager.setAdapter(new TabsAdapter(getChildFragmentManager())); tabLayout.setupWithViewPager(viewPager); @@ -123,6 +125,15 @@ public class ModulesFragment extends Fragment { } } + public static class DownloadModuleFragment extends BaseModuleFragment { + + @Override + protected List listModules() { + return listModulesDownload; + } + + } + private class updateUI extends AsyncTask { @@ -145,7 +156,7 @@ public class ModulesFragment extends Fragment { private class TabsAdapter extends FragmentPagerAdapter { String[] tabTitles = new String[]{ - getString(R.string.modules), getString(R.string.cache_modules) + getString(R.string.modules), getString(R.string.cache_modules) ,"Download" }; public TabsAdapter(FragmentManager fm) { @@ -166,9 +177,13 @@ public class ModulesFragment extends Fragment { public Fragment getItem(int position) { if (position == 0) { return new NormalModuleFragment(); - } else { + } else if (position == 1) { return new CacheModuleFragment(); + } else { + Log.d("Magisk","DL Fragment picked here"); + return new DownloadModuleFragment(); } } } + } diff --git a/app/src/main/java/com/topjohnwu/magisk/module/Module.java b/app/src/main/java/com/topjohnwu/magisk/module/Module.java index 74afbe3ab..6cfd62971 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/Module.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/Module.java @@ -74,6 +74,25 @@ public class Module { } + public Module(ModuleRepo.Repo repo) { + + + + mName = repo.getName(); + mVersion = repo.getVersion(); + mDescription = repo.getDescription(); + mId = "foo"; + mVersionCode = 111; + + + + + + mEnable = true; + mRemove = false; + + } + public String getName() { return mName; } diff --git a/app/src/main/java/com/topjohnwu/magisk/module/ModuleRepo.java b/app/src/main/java/com/topjohnwu/magisk/module/ModuleRepo.java new file mode 100644 index 000000000..ed564dc9f --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/module/ModuleRepo.java @@ -0,0 +1,170 @@ +package com.topjohnwu.magisk.module; + +import android.app.ListActivity; +import android.content.Context; +import android.os.AsyncTask; +import android.os.Bundle; +import android.util.Log; + +import com.topjohnwu.magisk.utils.WebRequest; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class ModuleRepo { + private String[] result; + private static String url = "https://api.github.com/orgs/Magisk-Modules-Repo/repos"; + private static List repos = new ArrayList(); + private static final String TAG_ID = "id"; + private static final String TAG_NAME = "name"; + private Context activityContext; + + + public List listRepos() { + MyAsyncTask asynchTask = new MyAsyncTask(); + Log.d("Magisk", "Gitagent init called"); + asynchTask.execute(); + List out = null; + + + + return repos; + } + + public JSONArray fetchModuleArray() { + fetchRepoInfo(); + parseRepoInfo(); + JSONArray moduleArray = enumerateModules(); + return null; + } + + private void fetchRepoInfo() { + + } + + private void parseRepoInfo() { + + } + + private JSONArray enumerateModules() { + JSONArray enumeratedModules = new JSONArray(); + return enumeratedModules; + } + + class MyAsyncTask extends AsyncTask { + + @Override + protected void onPreExecute() { + super.onPreExecute(); + + + } + + @Override + protected Void doInBackground(String... params) { + Log.d("Magisk", "doInBackground running"); + // Creating service handler class instance + WebRequest webreq = new WebRequest(); + + // Making a request to url and getting response + String jsonStr = webreq.makeWebServiceCall(url, WebRequest.GET); + + Log.d("Response: ", "> " + jsonStr); + + try { + repos.clear(); + JSONArray jsonArray = new JSONArray(jsonStr); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonobject = jsonArray.getJSONObject(i); + String name = jsonobject.getString("name"); + String urlString = jsonobject.getString("html_url"); + try { + URL url = new URL(urlString); + if (!name.contains("Repo.github.io")) { + repos.add(new Repo(name, url)); + } + } catch (MalformedURLException e) { + e.printStackTrace(); + } + + } + for (int i = 0; i < repos.size(); i++) { + Repo repo = repos.get(i); + Log.d("Magisk", repo.name + " URL: " + repo.url); + + } + } catch (JSONException e) { + e.printStackTrace(); + } + + + return null; + + + } + + protected void onPostExecute(Void v) { + + + } // protected void onPostExecute(Void v) + } //class MyAsyncTask extends AsyncTask + + protected void onPreExecute() { + + } + + public class Repo { + public String name; + public URL url; + public String manifest, version, moduleName, moduleDescription, moduleAuthor, moduleAuthorUrl; + public Boolean usesRoot,usesXposed; + + + public Repo(String name, URL url) { + this.name = name; + this.url = url; + this.manifest = ("https://raw.githubusercontent.com/Magisk-Modules-Repo/" + name + "/master/module.json"); + WebRequest webreq = new WebRequest(); + + // Making a request to url and getting response + String manifestString = webreq.makeWebServiceCall(manifest, WebRequest.GET); + Log.d("Magisk","Inner fetch: " + manifestString); + try { + JSONObject jsonobject = new JSONObject(manifestString); + Log.d("Magisk","Object: " +jsonobject.toString()); + version = jsonobject.getString("versionCode"); + moduleName = jsonobject.getString("moduleName"); + moduleDescription = jsonobject.getString("moduleDescription"); + moduleAuthor = jsonobject.getString("moduleAuthor"); + moduleAuthorUrl = jsonobject.getString("authorUrl"); + usesRoot = Boolean.getBoolean(jsonobject.getString("usesRoot")); + usesXposed = Boolean.getBoolean(jsonobject.getString("usesXposed")); + + } catch (JSONException e) { + e.printStackTrace(); + } + + Log.d("Magisk","We're in! " + " " + version + " " + moduleName + " " + moduleAuthor + " " + moduleDescription + " " + moduleAuthorUrl); + + } + + public String getName() { + return moduleName; + } + + public String getVersion() { + return version; + } + + public String getDescription() { + return moduleDescription; + } + } + +} diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 515bba62f..6a6e136f7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -16,12 +16,14 @@ import android.os.Environment; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AlertDialog; +import android.util.Log; import android.view.View; import android.widget.Toast; import com.topjohnwu.magisk.ModulesFragment; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.module.Module; +import com.topjohnwu.magisk.module.ModuleRepo; import org.json.JSONException; import org.json.JSONObject; @@ -370,14 +372,23 @@ public class Utils { protected Void doInBackground(Void... voids) { ModulesFragment.listModules.clear(); ModulesFragment.listModulesCache.clear(); + ModulesFragment.listModulesDownload.clear(); List magisk = getModList(MAGISK_PATH); + Log.d("Magisk", String.valueOf(magisk)); List magiskCache = getModList(MAGISK_CACHE_PATH); + ModuleRepo mr = new ModuleRepo(); + List magiskRepos = mr.listRepos(); for (String mod : magisk) { + Log.d("Magisk","Utils, listing modules " + mod); ModulesFragment.listModules.add(new Module(mod)); } for (String mod : magiskCache) { ModulesFragment.listModulesCache.add(new Module(mod)); } + for (ModuleRepo.Repo repo : magiskRepos) { + ModulesFragment.listModulesDownload.add(new Module(repo)); + } + return null; } } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/WebRequest.java b/app/src/main/java/com/topjohnwu/magisk/utils/WebRequest.java new file mode 100644 index 000000000..1b782a158 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/utils/WebRequest.java @@ -0,0 +1,105 @@ +package com.topjohnwu.magisk.utils; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.Map; + +import javax.net.ssl.HttpsURLConnection; + +public class WebRequest { + + static String response = null; + public final static int GET = 1; + public final static int POST = 2; + + //Constructor with no parameter + public WebRequest() { + + } + + /** + * Making web service call + * + * @url - url to make request + * @requestmethod - http request method + */ + public String makeWebServiceCall(String url, int requestmethod) { + return this.makeWebServiceCall(url, requestmethod, null); + } + + /** + * Making service call + * + * @url - url to make request + * @requestmethod - http request method + * @params - http request params + */ + public String makeWebServiceCall(String urladdress, int requestmethod, + HashMap params) { + URL url; + String response = ""; + try { + url = new URL(urladdress); + + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setReadTimeout(15000); + conn.setConnectTimeout(15000); + conn.setDoInput(true); + + if (requestmethod == POST) { + conn.setRequestMethod("POST"); + } else if (requestmethod == GET) { + conn.setRequestMethod("GET"); + } + + if (params != null) { + OutputStream os = conn.getOutputStream(); + BufferedWriter writer = new BufferedWriter( + new OutputStreamWriter(os, "UTF-8")); + + StringBuilder result = new StringBuilder(); + boolean first = true; + for (Map.Entry entry : params.entrySet()) { + if (first) + first = false; + else + result.append("&"); + + result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); + result.append("="); + result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); + } + + writer.write(result.toString()); + + writer.flush(); + writer.close(); + os.close(); + } + + int responseCode = conn.getResponseCode(); + + if (responseCode == HttpsURLConnection.HTTP_OK) { + String line; + BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + while ((line = br.readLine()) != null) { + response += line; + } + } else { + response = ""; + } + } catch (Exception e) { + e.printStackTrace(); + } + + return response; + } + +} \ No newline at end of file