diff --git a/app/src/main/java/com/topjohnwu/magisk/BaseModuleFragment.java b/app/src/main/java/com/topjohnwu/magisk/BaseModuleFragment.java index e41a73df8..530ad3e73 100644 --- a/app/src/main/java/com/topjohnwu/magisk/BaseModuleFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/BaseModuleFragment.java @@ -7,13 +7,16 @@ import android.support.annotation.Nullable; import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; +import android.widget.ImageView; import android.widget.TextView; import com.topjohnwu.magisk.module.Module; +import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; import java.util.List; @@ -23,17 +26,16 @@ import butterknife.ButterKnife; public abstract class BaseModuleFragment extends Fragment { - @BindView(R.id.recyclerView) - RecyclerView recyclerView; - @BindView(R.id.empty_rv) - TextView emptyTv; - private View view; + @BindView(R.id.recyclerView) RecyclerView recyclerView; + @BindView(R.id.empty_rv) TextView emptyTv; + + private SharedPreferences prefs; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - view = inflater.inflate(R.layout.single_module_fragment, container, false); + View view = inflater.inflate(R.layout.single_module_fragment, container, false); ButterKnife.bind(this, view); @@ -55,19 +57,16 @@ public abstract class BaseModuleFragment extends Fragment { return view; } - recyclerView.setAdapter(new ModulesAdapter(listModules(), new Utils.ItemClickListener() { - @Override - public void onItemClick(View chk, int position) { - // On Checkbox change listener - CheckBox chbox = (CheckBox) chk; + recyclerView.setAdapter(new ModulesAdapter(listModules(), (chk, position) -> { + // On Checkbox change listener + CheckBox chbox = (CheckBox) chk; - if (!chbox.isChecked()) { - BaseModuleFragment.this.listModules().get(position).createDisableFile(); - Snackbar.make(chk, R.string.disable_file_created, Snackbar.LENGTH_SHORT).show(); - } else { - BaseModuleFragment.this.listModules().get(position).removeDisableFile(); - Snackbar.make(chk, R.string.disable_file_removed, Snackbar.LENGTH_SHORT).show(); - } + if (!chbox.isChecked()) { + listModules().get(position).createDisableFile(); + Snackbar.make(chk, R.string.disable_file_created, Snackbar.LENGTH_SHORT).show(); + } else { + listModules().get(position).removeDisableFile(); + Snackbar.make(chk, R.string.disable_file_removed, Snackbar.LENGTH_SHORT).show(); } }, (deleteBtn, position) -> { // On delete button click listener @@ -85,4 +84,89 @@ public abstract class BaseModuleFragment extends Fragment { protected abstract List listModules(); + + public class ModulesAdapter extends RecyclerView.Adapter { + + private final List mList; + private final Utils.ItemClickListener chboxListener; + private final Utils.ItemClickListener deleteBtnListener; + private final Utils.ItemClickListener unDeleteBtnListener; + + public ModulesAdapter(List list, Utils.ItemClickListener chboxListener, Utils.ItemClickListener deleteBtnListener, Utils.ItemClickListener undeleteBtnListener) { + this.mList = list; + this.chboxListener = chboxListener; + this.deleteBtnListener = deleteBtnListener; + this.unDeleteBtnListener = undeleteBtnListener; + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_module, parent, false); + + return new ViewHolder(view); + } + + @Override + public void onBindViewHolder(final ViewHolder holder, int position) { + final Module module = mList.get(position); + Log.d("Magisk","ModulesAdapter: Trying set up bindview from list pos " + position + " and " + module.getName() ); + + holder.title.setText(module.getName()); + holder.versionName.setText(module.getVersion()); + holder.description.setText(module.getDescription()); + + holder.checkBox.setChecked(module.isEnabled()); + holder.checkBox.setOnCheckedChangeListener((compoundButton, b) -> chboxListener.onItemClick(compoundButton, holder.getAdapterPosition())); + + holder.delete.setOnClickListener(view -> { + if (module.willBeRemoved()) { + unDeleteBtnListener.onItemClick(holder.delete, holder.getAdapterPosition()); + } else { + deleteBtnListener.onItemClick(holder.delete, holder.getAdapterPosition()); + } + + updateDeleteButton(holder, module); + }); + + updateDeleteButton(holder, module); + } + + private void updateDeleteButton(ViewHolder holder, Module module) { + holder.warning.setVisibility(module.willBeRemoved() ? View.VISIBLE : View.GONE); + + if (module.willBeRemoved()) { + holder.delete.setImageResource(R.drawable.ic_undelete); + } else { + holder.delete.setImageResource(R.drawable.ic_delete); + } + } + + @Override + public int getItemCount() { + return mList.size(); + } + + class ViewHolder extends RecyclerView.ViewHolder { + + @BindView(R.id.title) TextView title; + + @BindView(R.id.version_name) TextView versionName; + @BindView(R.id.description) TextView description; + + @BindView(R.id.warning) TextView warning; + + @BindView(R.id.checkbox) CheckBox checkBox; + @BindView(R.id.delete) + ImageView delete; + + public ViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + if (!Shell.rootAccess()) { + checkBox.setEnabled(false); + delete.setEnabled(false); + } + } + } + } } diff --git a/app/src/main/java/com/topjohnwu/magisk/BaseRepoFragment.java b/app/src/main/java/com/topjohnwu/magisk/BaseRepoFragment.java index bc7d49474..082e14878 100644 --- a/app/src/main/java/com/topjohnwu/magisk/BaseRepoFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/BaseRepoFragment.java @@ -1,17 +1,26 @@ package com.topjohnwu.magisk; +import android.content.Context; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; -import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import com.topjohnwu.magisk.module.Repo; +import com.topjohnwu.magisk.utils.AnimationHelper; +import com.topjohnwu.magisk.utils.Utils; +import java.io.File; import java.util.List; import butterknife.BindView; @@ -26,6 +35,7 @@ public abstract class BaseRepoFragment extends Fragment { + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -40,7 +50,7 @@ public abstract class BaseRepoFragment extends Fragment { return view; } - + Log.d("Magisk","BaseRepoFragment: ListRepos size is " + listRepos().size()); recyclerView.setAdapter(new ReposAdapter(listRepos()) { }); @@ -49,4 +59,113 @@ public abstract class BaseRepoFragment extends Fragment { protected abstract List listRepos(); + + public class ReposAdapter extends RecyclerView.Adapter { + + private final List mList; + private View viewMain; + private Context context; + @BindView(R.id.update) + ImageView updateImage; + @BindView(R.id.installed) + ImageView installedImage; +// @BindView(R.id.popup_layout) +// LinearLayout popupLayout; + + + private boolean isCardExpanded; + private boolean mIsInstalled, mCanUpdate; + + public ReposAdapter(List list) { + this.mList = list; + + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + viewMain = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_repo, parent, false); + ButterKnife.bind(this, viewMain); + context = parent.getContext(); + return new ViewHolder(viewMain); + } + + @Override + public void onBindViewHolder(final ViewHolder holder, int position) { + final Repo repo = mList.get(position); + Log.d("Magisk","ReposAdapter: Trying set up bindview from list pos " + position + " out of a total of " + mList.size() + " and " + repo.getId() ); + if (repo.getId() != null) { + holder.title.setText(repo.getName()); + holder.versionName.setText(repo.getmVersion()); + holder.description.setText(repo.getDescription()); + Log.d("Magisk", "ReposAdapter: Setting up info " + repo.getId() + " and " + repo.getDescription() + " and " + repo.getmVersion()); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + if (prefs.contains("repo-isInstalled_" + repo.getId())) { + mIsInstalled = prefs.getBoolean("repo-isInstalled_" + repo.getId(), false); + if (mIsInstalled) { + installedImage.setImageResource(R.drawable.ic_done_black); + } + mCanUpdate = prefs.getBoolean("repo-isInstalled_" + repo.getId(), false); + if (mCanUpdate) { + updateImage.setImageResource(R.drawable.ic_system_update_alt_black); + } + } + + isCardExpanded = false; +// AnimationHelper.collapse(popupLayout); + + viewMain.setOnClickListener(new View.OnClickListener() { + @Override + + public void onClick(View view) { + if (isCardExpanded) { +// AnimationHelper.expand(popupLayout); + isCardExpanded = false; + } else { +// AnimationHelper.collapse(popupLayout); + isCardExpanded = true; + + } + +// if (!mIsInstalled | mCanUpdate) { +// +// Utils.DownloadReceiver reciever = new Utils.DownloadReceiver() { +// @Override +// public void task(File file) { +// Log.d("Magisk", "Task firing"); +// new Utils.FlashZIP(context, repo.getId(), file.toString()).execute(); +// } +// }; +// String filename = repo.getId().replace(" ", "") + ".zip"; +// Utils.downloadAndReceive(context, reciever, repo.getmZipUrl(), filename); +// } else { +// Toast.makeText(context,repo.getId() + " is already installed.", Toast.LENGTH_SHORT).show(); +// } + } + }); + } + + + } + + + + + @Override + public int getItemCount() { + return mList.size(); + } + + class ViewHolder extends RecyclerView.ViewHolder { + + @BindView(R.id.title) TextView title; + @BindView(R.id.version_name) TextView versionName; + @BindView(R.id.description) TextView description; + + + public ViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + } + } + } } diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesAdapter.java b/app/src/main/java/com/topjohnwu/magisk/ModulesAdapter.java deleted file mode 100644 index c5b015f4c..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/ModulesAdapter.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.topjohnwu.magisk; - -import android.support.v7.widget.RecyclerView; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.CheckBox; -import android.widget.ImageView; -import android.widget.TextView; - -import com.topjohnwu.magisk.module.Module; -import com.topjohnwu.magisk.utils.Shell; -import com.topjohnwu.magisk.utils.Utils; - -import java.util.List; - -import butterknife.BindView; -import butterknife.ButterKnife; - -public class ModulesAdapter extends RecyclerView.Adapter { - - private final List mList; - private final Utils.ItemClickListener chboxListener; - private final Utils.ItemClickListener deleteBtnListener; - private final Utils.ItemClickListener unDeleteBtnListener; - - public ModulesAdapter(List list, Utils.ItemClickListener chboxListener, Utils.ItemClickListener deleteBtnListener, Utils.ItemClickListener undeleteBtnListener) { - this.mList = list; - this.chboxListener = chboxListener; - this.deleteBtnListener = deleteBtnListener; - this.unDeleteBtnListener = undeleteBtnListener; - } - - @Override - public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_module, parent, false); - - return new ViewHolder(view); - } - - @Override - public void onBindViewHolder(final ViewHolder holder, int position) { - final Module module = mList.get(position); - - holder.title.setText(module.getName()); - holder.versionName.setText(module.getVersion()); - holder.description.setText(module.getDescription()); - - holder.checkBox.setChecked(module.isEnabled()); - holder.checkBox.setOnCheckedChangeListener((compoundButton, b) -> chboxListener.onItemClick(compoundButton, holder.getAdapterPosition())); - - holder.delete.setOnClickListener(view -> { - if (module.willBeRemoved()) { - unDeleteBtnListener.onItemClick(holder.delete, holder.getAdapterPosition()); - } else { - deleteBtnListener.onItemClick(holder.delete, holder.getAdapterPosition()); - } - - updateDeleteButton(holder, module); - }); - - updateDeleteButton(holder, module); - } - - private void updateDeleteButton(ViewHolder holder, Module module) { - holder.warning.setVisibility(module.willBeRemoved() ? View.VISIBLE : View.GONE); - - if (module.willBeRemoved()) { - holder.delete.setImageResource(R.drawable.ic_undelete); - } else { - holder.delete.setImageResource(R.drawable.ic_delete); - } - } - - @Override - public int getItemCount() { - return mList.size(); - } - - static class ViewHolder extends RecyclerView.ViewHolder { - - @BindView(R.id.title) TextView title; - - @BindView(R.id.version_name) TextView versionName; - @BindView(R.id.description) TextView description; - - @BindView(R.id.warning) TextView warning; - - @BindView(R.id.checkbox) CheckBox checkBox; - @BindView(R.id.delete) ImageView delete; - - public ViewHolder(View itemView) { - super(itemView); - ButterKnife.bind(this, itemView); - if (!Shell.rootAccess()) { - checkBox.setEnabled(false); - delete.setEnabled(false); - } - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java index 0af544180..6a2cc8ea0 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java @@ -56,23 +56,13 @@ public class ModulesFragment extends Fragment { View view = inflater.inflate(R.layout.modules_fragment, container, false); ButterKnife.bind(this, view); + //new Utils.LoadModules(getActivity(),false).execute(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); - if (prefs.contains("hasCachedRepos")) { - new Utils.LoadModules(getActivity(), false).execute(); - } else { - new Utils.LoadModules(getActivity(), true).execute(); - } - new updateUI().execute(); setHasOptionsMenu(true); return view; } - public void updateThisShit() { - new Utils.LoadModules(getActivity(), true).execute(); - new updateUI().execute(); - setHasOptionsMenu(true); - } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { @@ -93,8 +83,6 @@ public class ModulesFragment extends Fragment { } - - public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case FILE_SELECT_CODE: @@ -117,8 +105,7 @@ public class ModulesFragment extends Fragment { listModulesCache.clear(); listModulesDownload.clear(); progressBar.setVisibility(View.VISIBLE); - ta = new TabsAdapter(getChildFragmentManager()); - viewPager.setAdapter(ta); + viewPager.setAdapter(new TabsAdapter(getChildFragmentManager())); tabLayout.setupWithViewPager(viewPager); new Utils.LoadModules(getActivity(),true).execute(); new updateUI().execute(); @@ -128,19 +115,6 @@ public class ModulesFragment extends Fragment { return super.onOptionsItemSelected(item); } - public void redrawLayout() { - listModules.clear(); - listModulesCache.clear(); - listModulesDownload.clear(); - progressBar.setVisibility(View.VISIBLE); - ta = new TabsAdapter(getChildFragmentManager()); - viewPager.setAdapter(ta); - tabLayout.setupWithViewPager(viewPager); - new Utils.LoadModules(getActivity(),false).execute(); - new updateUI().execute(); - - } - public static class NormalModuleFragment extends BaseModuleFragment { @Override @@ -168,7 +142,7 @@ public class ModulesFragment extends Fragment { } - public class updateUI extends AsyncTask { + private class updateUI extends AsyncTask { @Override protected Void doInBackground(Void... voids) { @@ -180,8 +154,8 @@ public class ModulesFragment extends Fragment { super.onPostExecute(v); progressBar.setVisibility(View.GONE); - ta = new TabsAdapter(getChildFragmentManager()); - viewPager.setAdapter(ta); + + viewPager.setAdapter(new TabsAdapter(getChildFragmentManager())); tabLayout.setupWithViewPager(viewPager); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/ReposAdapter.java b/app/src/main/java/com/topjohnwu/magisk/ReposAdapter.java deleted file mode 100644 index 5d65987a6..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/ReposAdapter.java +++ /dev/null @@ -1,140 +0,0 @@ -package com.topjohnwu.magisk; - -import android.content.Context; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.support.v7.widget.CardView; -import android.support.v7.widget.RecyclerView; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.LinearLayout; -import android.widget.TextView; - -import com.topjohnwu.magisk.module.Repo; -import com.topjohnwu.magisk.utils.AnimationHelper; - -import org.w3c.dom.Text; - -import java.util.List; - -import butterknife.BindView; -import butterknife.ButterKnife; - -public class ReposAdapter extends RecyclerView.Adapter { - - private final List mList; - private View viewMain; - private Context context; - @BindView(R.id.update) - ImageView updateImage; - @BindView(R.id.installed) - ImageView installedImage; - @BindView(R.id.popup_layout) - LinearLayout popupLayout; - @BindView(R.id.author) - TextView authorText; - @BindView(R.id.log) - TextView logText; - @BindView(R.id.updateStatus) TextView updateStatus; - @BindView(R.id.installedStatus) TextView installedStatus; - private boolean isCardExpanded; - - - public ReposAdapter(List list) { - this.mList = list; - - } - - private boolean mIsInstalled; - - - - - @Override - public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - viewMain = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_repo, parent, false); - ButterKnife.bind(this, viewMain); - context = parent.getContext(); - return new ViewHolder(viewMain); - } - - @Override - public void onBindViewHolder(final ViewHolder holder, int position) { - final Repo repo = mList.get(position); - - holder.title.setText(repo.getName()); - holder.versionName.setText(repo.getmVersion()); - holder.description.setText(repo.getDescription()); - Log.d("Magisk","ReposAdapter: Setting up info " + repo.getId() + " and " + repo.getDescription() + " and " + repo.getmVersion()); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (prefs.contains("repo_isInstalled_" + repo.getId())) { - mIsInstalled = prefs.getBoolean("repo_isInstalled_" + repo.getId(),false); - if (mIsInstalled) { - installedImage.setImageResource(R.drawable.ic_done_black); - installedStatus.setText(R.string.module_installed); - } - } - - isCardExpanded = false; - AnimationHelper.collapse(popupLayout); - - viewMain.setOnClickListener(new View.OnClickListener() { - @Override - - public void onClick(View view) { - if (isCardExpanded) { - - AnimationHelper.collapse(popupLayout); - isCardExpanded = false; - } else { - AnimationHelper.expand(popupLayout); - isCardExpanded = true; - - } - -// if (!mIsInstalled) { -// -// Utils.DownloadReceiver reciever = new Utils.DownloadReceiver() { -// @Override -// public void task(File file) { -// Log.d("Magisk", "Task firing"); -// new Utils.FlashZIP(context, repo.getId(), file.toString()).execute(); -// } -// }; -// String filename = repo.getId().replace(" ", "") + ".zip"; -// Utils.downloadAndReceive(context, reciever, repo.getmZipUrl(), filename); -// } else { -// Toast.makeText(context,repo.getId() + " is already installed.",Toast.LENGTH_SHORT).show(); -// } - } - }); - - - } - - - - - @Override - public int getItemCount() { - return mList.size(); - } - - static class ViewHolder extends RecyclerView.ViewHolder { - - @BindView(R.id.title) TextView title; - - @BindView(R.id.version_name) TextView versionName; - @BindView(R.id.description) TextView description; - - - - public ViewHolder(View itemView) { - super(itemView); - ButterKnife.bind(this, itemView); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java b/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java index 6d34d7cfd..cb3c30bde 100644 --- a/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java @@ -4,10 +4,12 @@ import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.preference.PreferenceManager; import android.support.annotation.IdRes; import android.support.annotation.NonNull; import android.support.design.widget.NavigationView; @@ -58,7 +60,15 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView } new Utils.Initialize(this).execute(); new Utils.CheckUpdates(this).execute(); - new Utils.LoadModules(getApplication(),false).execute(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + if (!prefs.contains("oauth_key")) { + + } + if (!prefs.contains("hasCachedRepos")) { + new Utils.LoadModules(this, true).execute(); + } else { + new Utils.LoadModules(getApplication(),false).execute(); + } setSupportActionBar(toolbar); 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 0e20a1294..f5ab7f1e9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/Module.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/Module.java @@ -45,90 +45,98 @@ public class Module { this.mVersionCode = Integer.valueOf(props[1]); break; case "name": - this.mName = props[1]; + this.mName = value; break; case "author": - this.mAuthor = props[1]; + this.mAuthor = value; break; case "id": - this.mId = props[1]; + this.mId = value; break; case "version": - this.mVersion = props[1]; + this.mVersion = value; break; case "description": - this.mDescription = props[1]; + this.mDescription = value; break; case "donate": - this.mDonateUrl = props[1]; + this.mDonateUrl = value; break; case "support": - this.mSupportUrl = props[1]; + this.mSupportUrl = value; break; case "donateUrl": - this.mDonateUrl = props[1]; + this.mDonateUrl = value; break; case "zipUrl": - this.mZipUrl = props[1]; + this.mZipUrl = value; break; case "baseUrl": - this.mBaseUrl = props[1]; + this.mBaseUrl = value; break; case "manifestUrl": - this.mManifestUrl = props[1]; + this.mManifestUrl = value; break; default: - Log.d("Magisk", "Manifest string not recognized: " + props[0]); + Log.d("Magisk", "Module: Manifest string not recognized: " + props[0]); break; } - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (mId != null) { - Log.d("Magisk", "Module: Checking for preference named repo_" + mId); - if (prefs.contains("repo_" + mId)) { - String entryString = prefs.getString("repo_" + mId, ""); + } - String entryName = "repo" + mId; + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - String[] subStrings = entryString.split("\n"); - for (String subKeys : subStrings) { - String[] idEntry = subKeys.split("=", 2); - Log.d("Magisk", "Module: Checking entry strings. Key is " + idEntry[0] + " and value is " + idEntry[1]); - if (idEntry[0].equals("id")) { - if (idEntry.length != 2) { - continue; - } + if (this.mId != null && !this.mId.isEmpty()) { + String preferenceString = "repo_" + this.mId; + String preferenceKey = prefs.getString(preferenceString,"nope"); + Log.d("Magisk", "Module: Checking for preference named " + preferenceString); + if (!preferenceKey.equals("nope")) { + Log.d("Magisk", "Module: repo_" + mId + " found."); + String entryString = prefs.getString("repo_" + mId, ""); - if (idEntry[1].equals(mId)) { - Log.d("Magisk", "Module: Hey, I know I'm online..."); - mIsOnline = true; - } else mIsOnline = false; + String[] subStrings = entryString.split("\n"); + for (String subKeys : subStrings) { + String[] idEntry = subKeys.split("=", 2); + if (idEntry[0].equals("id")) { + if (idEntry.length != 2) { + continue; } - if (idEntry[0].equals("versionCode")) { - if (idEntry.length != 2) { - continue; - } - if (Integer.valueOf(idEntry[1]) > mVersionCode) { - mUpdateAvailable = true; - Log.d("Magisk", "Module: Hey, I have an update..."); - } else mUpdateAvailable = false; - } + if (idEntry[1].equals(mId)) { + Log.d("Magisk", "Module: Hey, I know I'm online..."); + mIsOnline = true; + } else mIsOnline = false; } + if (idEntry[0].equals("versionCode")) { + if (idEntry.length != 2) { + continue; + } - + if (Integer.valueOf(idEntry[1]) > mVersionCode) { + mUpdateAvailable = true; + Log.d("Magisk", "Module: Hey, I have an update..."); + } else mUpdateAvailable = false; + } } - SharedPreferences.Editor editor = prefs.edit(); - if (mIsOnline) { - editor.putBoolean("repo_isInstalled_" + mId, true); - } else { - editor.putBoolean("repo_isInstalled_" + mId, false); - } - editor.apply(); } + + SharedPreferences.Editor editor = prefs.edit(); + if (mIsOnline) { + editor.putBoolean("repo-isInstalled_" + mId, true); + + } else { + editor.putBoolean("repo-isInstalled_" + mId, false); + } + + if (mUpdateAvailable) { + editor.putBoolean("repo-canUpdate_" + mId, true); + } else { + editor.putBoolean("repo-canUpdate_" + mId, false); + } + editor.apply(); } if (mName == null) { @@ -147,8 +155,8 @@ public class Module { mName = repo.getName(); mVersion = repo.getmVersion(); mDescription = repo.getDescription(); - mId = "foo"; - mVersionCode = 111; + mId = repo.getId(); + mVersionCode = repo.getmVersionCode(); mUrl = repo.getmZipUrl(); mEnable = true; mRemove = false; diff --git a/app/src/main/java/com/topjohnwu/magisk/module/Repo.java b/app/src/main/java/com/topjohnwu/magisk/module/Repo.java index e6944915f..5814ab04c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/Repo.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/Repo.java @@ -28,21 +28,23 @@ public class Repo { private String mVersionCode; private String mSupportUrl; private String mDonateUrl; - private Date lastUpdate; + private String lastUpdate; private Context appContext; - private boolean mIsInstalled; + private boolean mIsInstalled,mCanUpdate; public Repo(String manifestString, Context context) { - ParseProps(manifestString); appContext = context; + ParseProps(manifestString); } + + public Repo(String name, String url, Date updated, Context context) { appContext = context; this.mName = name; this.mBaseUrl = url; - this.lastUpdate = updated; + this.lastUpdate = updated.toString(); this.fetch(); } @@ -52,28 +54,16 @@ public class Repo { this.mZipUrl = zipUrl; this.mDescription = moduleDescription; this.mName = moduleName; - this.lastUpdate = lastUpdated; + this.lastUpdate = lastUpdated.toString(); this.fetch(); } - private void fetch() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext); - if (prefs.contains("repo_" + this.mId)) { - String repoString = prefs.getString("repo_" + this.mId,""); - if (!repoString.equals("")) { - ParseProps(repoString); - } - } - if (prefs.contains("repo_isInstalled_" + this.mId)) { - mIsInstalled = prefs.getBoolean("repo_isInstalled_" + this.mId,false); - - } - + public void fetch() { WebRequest webreq = new WebRequest(); // Construct initial url for contents - Log.d("Magisk", "Manifest string is: " + mBaseUrl + "/contents/"); - String repoString = webreq.makeWebServiceCall(mBaseUrl + "/contents/", WebRequest.GET); + Log.d("Magisk", "Repo: Fetch called, Manifest string is: " + mBaseUrl + "/contents?access_token=5c9f47a299d48a6a649af3587bc97200bafcac65"); + String repoString = webreq.makeWebServiceCall(mBaseUrl + "/contents?access_token=5c9f47a299d48a6a649af3587bc97200bafcac65", WebRequest.GET); try { JSONArray repoArray = new JSONArray(repoString); for (int f = 0; f < repoArray.length(); f++) { @@ -91,9 +81,11 @@ public class Repo { e.printStackTrace(); } - Log.d("Magisk", "Inner fetch: " + repoString); + Log.d("Magisk", "Repo: Inner fetch: " + mManifestUrl + "?access_token=5c9f47a299d48a6a649af3587bc97200bafcac65"); WebRequest propReq = new WebRequest(); - String manifestString = propReq.makeWebServiceCall(this.mManifestUrl,WebRequest.GET,true); + String manifestString = propReq.makeWebServiceCall(mManifestUrl,WebRequest.GET,true); + Log.d("Magisk","Repo: parseprops called from fetch for string " + manifestString); + if (ParseProps(manifestString)) { PutProps(manifestString); } @@ -111,7 +103,7 @@ public class Repo { editor.apply(); } private boolean ParseProps(String string) { - Log.d("Magisk","Repo: parseprops called for string " + string); + if ((string.length() <= 1) | (!string.contains("id"))) { return false; } else { @@ -119,7 +111,6 @@ public class Repo { for (String line : lines) { if (line != "") { String props[] = line.split("="); - Log.d("Magisk", "Repo: Split values are " + props[0] + " and " + props[1]); switch (props[0]) { case "versionCode": this.mVersionCode = props[1]; @@ -157,6 +148,9 @@ public class Repo { case "manifestUrl": this.mManifestUrl = props[1]; break; + case "logUrl": + this.mLogUrl = props[1]; + break; default: Log.d("Magisk", "Manifest string not recognized: " + props[0]); break; @@ -164,7 +158,20 @@ public class Repo { } } - return this.mName != null; + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext); + if (prefs.contains("repo-isInstalled_" + this.mId)) { + mIsInstalled = prefs.getBoolean("repo-isInstalled_" + this.mId,false); + } + if (prefs.contains("repo-canUpdate_" + this.mId)) { + mCanUpdate = prefs.getBoolean("repo-canUpdate_" + this.mId,false); + } + if (prefs.contains("updated_" + this.mId)) { + lastUpdate = prefs.getString("updated_" + this.mId,""); + } + + + + return this.mId != null; } } @@ -241,10 +248,11 @@ public class Repo { return mSupportUrl; } - public Date getLastUpdate() { + public String getLastUpdate() { return lastUpdate; } public boolean isInstalled() { return mIsInstalled; } + public boolean canUpdate() { return mCanUpdate; } } diff --git a/app/src/main/java/com/topjohnwu/magisk/module/RepoAdapter.java b/app/src/main/java/com/topjohnwu/magisk/module/RepoAdapter.java deleted file mode 100644 index c5e3b9c0d..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/module/RepoAdapter.java +++ /dev/null @@ -1,135 +0,0 @@ -package com.topjohnwu.magisk.module; - -import android.content.Context; -import android.content.SharedPreferences; -import android.os.AsyncTask; -import android.os.Build; -import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.util.Log; -import android.widget.Toast; - -import com.topjohnwu.magisk.utils.Utils; -import com.topjohnwu.magisk.utils.WebRequest; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.File; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -public class RepoAdapter { - 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 static String TAG = "Magisk"; - private Context activityContext; - private Date updatedDate, currentDate; - - public List listRepos(Context context, boolean refresh) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (!prefs.contains("hasCachedRepos") | refresh) { - activityContext = context; - new MyAsyncTask().execute(); - List out = null; - } else { - Log.d(TAG, "Building from cache"); - Map map = prefs.getAll(); - repos.clear(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - map.entrySet().stream().filter(entry -> entry.getKey().contains("repo_")).forEach(entry -> { - String repoString = entry.getValue().toString(); - if (repoString.length() >= 0) { - repos.add(new Repo(repoString,context)); - } - - }); - } - - } - - - return repos; - } - - - class MyAsyncTask extends AsyncTask { - - @Override - protected void onPreExecute() { - super.onPreExecute(); - - } - - @Override - protected void onProgressUpdate(String... values) { - super.onProgressUpdate(values); - Toast.makeText(activityContext, "Refreshing online modules", Toast.LENGTH_SHORT).show(); - - } - - @Override - protected Void doInBackground(String... params) { - publishProgress(); - // 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("Magisk", "doInBackground Running, String: " + jsonStr + " Url: " + url); - - - 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 url = jsonobject.getString("url"); - String lastUpdate = jsonobject.getString("updated_at"); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - try { - updatedDate = format.parse(lastUpdate); - - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - if ((!name.contains("Repo.github.io"))) { - //if (!name.contains("Repo.github.io") && name.contains("template")) { - repos.add(new Repo(name, url, updatedDate, activityContext)); - } - } - } catch (JSONException e) { - e.printStackTrace(); - } - return null; - - - } - - protected void onPostExecute(Void v) { - - - } // protected void onPostExecute(Void v) - } //class MyAsyncTask extends AsyncTask - - protected void onPreExecute() { - - } - - -} diff --git a/app/src/main/java/com/topjohnwu/magisk/module/RepoHelper.java b/app/src/main/java/com/topjohnwu/magisk/module/RepoHelper.java new file mode 100644 index 000000000..d3fdfc0e9 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/module/RepoHelper.java @@ -0,0 +1,197 @@ +package com.topjohnwu.magisk.module; + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.AsyncTask; +import android.os.Build; +import android.preference.PreferenceManager; +import android.util.Log; +import android.widget.Toast; + +import com.topjohnwu.magisk.utils.WebRequest; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; + +public class RepoHelper { + private String[] result; + private static String url = "https://api.github.com/orgs/Magisk-Modules-Repo/repos?access_token=5c9f47a299d48a6a649af3587bc97200bafcac65"; + private static List repos = new ArrayList(); + private static final String TAG_ID = "id"; + private static final String TAG_NAME = "name"; + private static String TAG = "Magisk"; + private String mName,mId,mUrl; + private Context activityContext; + private Date updatedDate, currentDate; + private SharedPreferences prefs; + private boolean apiFail; + + public List listRepos(Context context, boolean refresh) { + prefs = PreferenceManager.getDefaultSharedPreferences(context); + activityContext = context; + if (!prefs.contains("hasCachedRepos") | refresh) { + Log.d(TAG, "RepoHelper: Building from web"); + new MyAsyncTask().execute(); + List out = null; + } else { + Log.d(TAG, "RepoHelper: Building from cache"); + repos.clear(); + Map map = prefs.getAll(); + for(Map.Entry entry : map.entrySet()){ + if (entry.getKey().contains("repo_")) { + Log.d("Magisk","RepoHelper: found entry for repo " + entry.getKey()); + String repoString = entry.getValue().toString().replace(""", "\""); + String[] repoStrings = repoString.split("\n"); + for(String string : repoStrings) { + String[] splitStrings = string.split("="); + switch(splitStrings[0]) { + case ("id"): + mId = splitStrings[1]; + break; + case ("baseUrl"): + mUrl = splitStrings[1]; + break; + default: + break; + } + + } + Log.d("Magisk","RepoHelper: adding repo with id of " + mId); + repos.add(new Repo(repoString,activityContext)); + + + } + + } + + + + } + + + return repos; + } + + + class MyAsyncTask extends AsyncTask { + + @Override + protected void onPreExecute() { + super.onPreExecute(); + + } + + @Override + protected void onProgressUpdate(String... values) { + super.onProgressUpdate(values); + Toast.makeText(activityContext, "Refreshing online modules", Toast.LENGTH_SHORT).show(); + + } + + @Override + protected Void doInBackground(String... params) { + publishProgress(); + // 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("Magisk", "doInBackground Running, String: " + jsonStr + " Url: " + url); + if(jsonStr != null && !jsonStr.isEmpty()) { + + 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 url = jsonobject.getString("url"); + String lastUpdate = jsonobject.getString("updated_at"); + String mId = ""; + String cacheUpdate = ""; + String manifestString = ""; + boolean doUpdate = true; + boolean hasCachedDate = false; + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + Map map = prefs.getAll(); + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue().toString().contains(url)) { + Log.d("Magisk", "RepoHelper: found matching URL"); + manifestString = entry.getValue().toString(); + String[] entryStrings = entry.getValue().toString().split("\n"); + for (String valueString : entryStrings) { + String[] valueSub = valueString.split("="); + if (valueSub[0].equals("id")) { + mId = valueSub[1]; + Log.d("Magisk", "RepoHelper: Got id for package of " + mId); + if (prefs.contains("updated_" + mId)) { + cacheUpdate = prefs.getString("updated_" + mId, ""); + hasCachedDate = true; + } + } + } + + } + } + try { + updatedDate = format.parse(lastUpdate); + Log.d("Magisk", "RepoHelper: Dates found, online is " + updatedDate + " and cached is " + cacheUpdate); + + if (hasCachedDate) { + + doUpdate = !cacheUpdate.equals(updatedDate.toString()); + Log.d("Magisk", "RepoHelper: DoUpdate is " + doUpdate); + } + + + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + if (!name.contains("Repo.github.io")) { + if (doUpdate) { + repos.add(new Repo(name, url, updatedDate, activityContext)); + Log.d(TAG, "RepoHelper: Trying to add new repo for online refresh - " + name); + } else { + repos.add(new Repo(manifestString, activityContext)); + Log.d(TAG, "RepoHelper: Trying to add new repo using manifestString of " + mId); + } + } + for (int f = 0; f < repos.size(); f++) { + repos.get(f).fetch(); + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + apiFail = false; + } else { + apiFail = true; + } + return null; + + + } + + protected void onPostExecute(Void v) { + if (apiFail) { + Toast.makeText(activityContext,"GitHub API Limit reached, please try refreshing again in an hour.",Toast.LENGTH_LONG).show(); + } + + } // protected void onPostExecute(Void v) + } //class MyAsyncTask extends AsyncTask + + protected void onPreExecute() { + + } + + +} 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 8c4322174..c4fdfb4f5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -25,7 +25,7 @@ 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.RepoAdapter; +import com.topjohnwu.magisk.module.RepoHelper; import com.topjohnwu.magisk.module.Repo; import org.json.JSONArray; @@ -38,16 +38,14 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.List; -import java.util.Set; public class Utils { public static int magiskVersion, remoteMagiskVersion = -1, remoteAppVersion = -1; public static String magiskLink, magiskChangelog, appChangelog, appLink, phhLink, supersuLink; private Context appContext; + private static final String TAG = "Magisk"; public static final String MAGISK_PATH = "/magisk"; public static final String MAGISK_CACHE_PATH = "/cache/magisk"; @@ -373,17 +371,25 @@ public class Utils { } } + public static class AuthGithub extends AsyncTask { + private static String mClientToken = "5c9f47a299d48a6a649af3587bc97200bafcac65"; + + public AuthGithub(Context context) {} + + @Override + protected Void doInBackground(Void... voids) { + return null; + } + } + public static class LoadModules extends AsyncTask { private Context mContext; private boolean doReload; public LoadModules(Context context, boolean reload) { - Log.d("Magisk", "LoadModules created, online is " + reload); mContext = context; doReload = reload; - - } @Override @@ -392,30 +398,26 @@ public class Utils { ModulesFragment.listModulesCache.clear(); ModulesFragment.listModulesDownload.clear(); List magisk = getModList(MAGISK_PATH); - Log.d("Magisk", "Reload called, online mode set to " + doReload); + Log.d("Magisk", "Utils: Reload called, loading modules from" + (doReload ? " the internet " : " cache")); List magiskCache = getModList(MAGISK_CACHE_PATH); - RepoAdapter mr = new RepoAdapter(); + RepoHelper mr = new RepoHelper(); List magiskRepos = mr.listRepos(mContext, doReload); for (String mod : magisk) { + Log.d("Magisk","Utils: Adding module from string " + mod); ModulesFragment.listModules.add(new Module(mod,mContext)); } for (String mod : magiskCache) { + Log.d("Magisk","Utils: Adding cache module from string " + mod); ModulesFragment.listModulesCache.add(new Module(mod,mContext)); } for (Repo repo : magiskRepos) { - if (repo.getId() != null){ - ModulesFragment.listModulesDownload.add(repo); - } + Log.d("Magisk","Utils: Adding repo from string " + repo.getId()); + ModulesFragment.listModulesDownload.add(repo); } return null; } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - } } public static class FlashZIP extends AsyncTask { @@ -462,35 +464,6 @@ public class Utils { if (!result) { Toast.makeText(mContext, mContext.getString(R.string.manual_install, mPath), Toast.LENGTH_LONG).show(); return; - } else { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); - String jsonString = prefs.getString("module_" + mName,""); - String retSplit[] = ret.toString().split("Using path:"); - String ret2Split[] = retSplit[1].split(","); - String ret3Split[] = ret2Split[0].split("/"); - String finalSplit = "/" + ret3Split[1] + "/" + ret3Split[2]; - Log.d("Magisk","Damn, all that work for one path " + finalSplit); - if (!jsonString.equals("")) { - - JSONArray repoArray = null; - try { - repoArray = new JSONArray(jsonString); - - - for (int f = 0; f < repoArray.length(); f++) { - JSONObject jsonobject = repoArray.getJSONObject(f); - String name = mName; - Boolean installed = true; - SharedPreferences.Editor editor = prefs.edit(); - editor.putBoolean("isInstalled_" + mName,true); - editor.putString("path_" + mName,finalSplit); - editor.apply(); - - } - } catch (JSONException e) { - e.printStackTrace(); - } - } } done(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/WebRequest.java b/app/src/main/java/com/topjohnwu/magisk/utils/WebRequest.java index 171ac5a8a..d01649e97 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/WebRequest.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/WebRequest.java @@ -1,5 +1,7 @@ package com.topjohnwu.magisk.utils; +import android.util.Log; + import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; @@ -33,12 +35,15 @@ public class WebRequest { */ public String makeWebServiceCall(String url, int requestmethod) { addNewLine=false; + Log.d("Magisk","WebRequest: Service call received for URL " + url); return this.makeWebServiceCall(url, requestmethod, null); + } public String makeWebServiceCall(String url, int requestmethod, boolean addNewLines) { addNewLine = addNewLines; + Log.d("Magisk","WebRequest: Service call(bool) received for URL " + url); return this.makeWebServiceCall(url, requestmethod, null); } @@ -99,7 +104,11 @@ public class WebRequest { String line; BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = br.readLine()) != null) { - response += line + "\n"; + if (addNewLine) { + response += line + "\n"; + } else { + response += line; + } } } else { response = ""; diff --git a/app/src/main/res/drawable/ic_system_update_alt_black.xml b/app/src/main/res/drawable/ic_system_update_alt_black.xml new file mode 100644 index 000000000..25b07b5b4 --- /dev/null +++ b/app/src/main/res/drawable/ic_system_update_alt_black.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/list_item_repo.xml b/app/src/main/res/layout/list_item_repo.xml index 4eea607f8..71f9d461e 100644 --- a/app/src/main/res/layout/list_item_repo.xml +++ b/app/src/main/res/layout/list_item_repo.xml @@ -1,8 +1,5 @@ - + - + - \ No newline at end of file