2016-08-21 13:29:42 +00:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
2016-08-25 15:39:09 +00:00
|
|
|
import android.Manifest;
|
2016-08-25 12:36:06 +00:00
|
|
|
import android.app.ProgressDialog;
|
2016-08-25 12:59:07 +00:00
|
|
|
import android.content.Context;
|
2016-08-25 12:36:06 +00:00
|
|
|
import android.content.DialogInterface;
|
2016-08-23 15:02:32 +00:00
|
|
|
import android.content.Intent;
|
2016-08-25 12:36:06 +00:00
|
|
|
import android.content.SharedPreferences;
|
2016-08-25 15:39:09 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2016-08-25 12:36:06 +00:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.AsyncTask;
|
2016-08-21 15:42:42 +00:00
|
|
|
import android.os.Build;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.os.Bundle;
|
2016-08-25 15:39:09 +00:00
|
|
|
import android.os.Environment;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.os.Handler;
|
2016-08-21 15:21:37 +00:00
|
|
|
import android.support.annotation.IdRes;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.design.widget.NavigationView;
|
2016-08-25 15:39:09 +00:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.app.FragmentTransaction;
|
|
|
|
import android.support.v4.view.GravityCompat;
|
|
|
|
import android.support.v4.widget.DrawerLayout;
|
|
|
|
import android.support.v7.app.ActionBarDrawerToggle;
|
2016-08-25 12:36:06 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
2016-08-25 12:59:07 +00:00
|
|
|
import android.text.Html;
|
2016-08-25 12:36:06 +00:00
|
|
|
import android.text.TextUtils;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
2016-08-25 12:36:06 +00:00
|
|
|
import android.widget.Toast;
|
2016-08-21 13:29:42 +00:00
|
|
|
|
2016-08-25 12:36:06 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Shell;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
import java.io.BufferedInputStream;
|
2016-08-25 12:36:06 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
2016-08-25 12:59:07 +00:00
|
|
|
import java.io.InputStream;
|
2016-08-25 12:36:06 +00:00
|
|
|
import java.io.InputStreamReader;
|
2016-08-25 12:59:07 +00:00
|
|
|
import java.io.OutputStream;
|
2016-08-25 12:36:06 +00:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLConnection;
|
2016-08-25 12:59:07 +00:00
|
|
|
import java.util.List;
|
2016-08-24 21:58:15 +00:00
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public class WelcomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
|
|
|
|
|
|
|
|
private static final String SELECTED_ITEM_ID = "SELECTED_ITEM_ID";
|
2016-08-25 12:59:07 +00:00
|
|
|
private static final String JSON_UPDATE_CHECK = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/updater/app/magisk_update.json";
|
|
|
|
|
2016-08-23 09:39:18 +00:00
|
|
|
private final Handler mDrawerHandler = new Handler();
|
2016-08-25 09:04:56 +00:00
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
@BindView(R.id.toolbar) Toolbar toolbar;
|
|
|
|
@BindView(R.id.drawer_layout) DrawerLayout drawer;
|
|
|
|
@BindView(R.id.nav_view) NavigationView navigationView;
|
|
|
|
|
2016-08-21 15:21:37 +00:00
|
|
|
@IdRes
|
2016-08-23 09:39:18 +00:00
|
|
|
private int mSelectedId = R.id.magisk;
|
2016-08-22 21:18:28 +00:00
|
|
|
|
2016-08-25 15:39:09 +00:00
|
|
|
private String mLastLink;
|
|
|
|
private boolean mLastIsApp;
|
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_welcome);
|
|
|
|
ButterKnife.bind(this);
|
2016-08-23 16:28:27 +00:00
|
|
|
|
2016-08-21 15:42:42 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
|
|
|
}
|
2016-08-21 13:29:42 +00:00
|
|
|
|
2016-08-25 10:08:07 +00:00
|
|
|
// Load mods in the background
|
|
|
|
ModulesFragment.loadMod = new ModulesFragment.loadModules();
|
|
|
|
ModulesFragment.loadMod.execute();
|
2016-08-22 19:50:46 +00:00
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
|
|
|
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
|
|
|
|
@Override
|
|
|
|
public void onDrawerOpened(View drawerView) {
|
|
|
|
super.onDrawerOpened(drawerView);
|
|
|
|
super.onDrawerSlide(drawerView, 0); // this disables the arrow @ completed state
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDrawerSlide(View drawerView, float slideOffset) {
|
|
|
|
super.onDrawerSlide(drawerView, 0); // this disables the animation
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-21 15:21:37 +00:00
|
|
|
drawer.addDrawerListener(toggle);
|
2016-08-21 13:29:42 +00:00
|
|
|
toggle.syncState();
|
|
|
|
|
2016-08-21 15:21:37 +00:00
|
|
|
//noinspection ResourceType
|
|
|
|
mSelectedId = savedInstanceState == null ? mSelectedId : savedInstanceState.getInt(SELECTED_ITEM_ID);
|
|
|
|
navigationView.setCheckedItem(mSelectedId);
|
2016-08-21 13:29:42 +00:00
|
|
|
|
2016-08-21 15:21:37 +00:00
|
|
|
if (savedInstanceState == null) {
|
2016-08-21 13:29:42 +00:00
|
|
|
mDrawerHandler.removeCallbacksAndMessages(null);
|
|
|
|
mDrawerHandler.postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2016-08-21 15:21:37 +00:00
|
|
|
navigate(mSelectedId);
|
2016-08-21 13:29:42 +00:00
|
|
|
}
|
|
|
|
}, 250);
|
|
|
|
}
|
|
|
|
|
2016-08-21 15:21:37 +00:00
|
|
|
navigationView.setNavigationItemSelectedListener(this);
|
2016-08-25 09:04:56 +00:00
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
new CheckUpdates(this).execute();
|
2016-08-21 15:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
|
|
|
|
outState.putInt(SELECTED_ITEM_ID, mSelectedId);
|
2016-08-21 13:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
if (drawer.isDrawerOpen(GravityCompat.START)) {
|
|
|
|
drawer.closeDrawer(GravityCompat.START);
|
|
|
|
} else {
|
|
|
|
super.onBackPressed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onNavigationItemSelected(@NonNull final MenuItem menuItem) {
|
2016-08-21 15:21:37 +00:00
|
|
|
mSelectedId = menuItem.getItemId();
|
2016-08-21 13:29:42 +00:00
|
|
|
mDrawerHandler.removeCallbacksAndMessages(null);
|
|
|
|
mDrawerHandler.postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
navigate(menuItem.getItemId());
|
|
|
|
}
|
|
|
|
}, 250);
|
|
|
|
|
|
|
|
drawer.closeDrawer(GravityCompat.START);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void navigate(final int itemId) {
|
|
|
|
Fragment navFragment = null;
|
2016-08-23 16:28:27 +00:00
|
|
|
String tag = "";
|
2016-08-21 13:29:42 +00:00
|
|
|
switch (itemId) {
|
2016-08-23 09:39:18 +00:00
|
|
|
case R.id.magisk:
|
|
|
|
setTitle(R.string.magisk);
|
2016-08-23 16:28:27 +00:00
|
|
|
tag = "magisk";
|
2016-08-23 09:39:18 +00:00
|
|
|
navFragment = new MagiskFragment();
|
|
|
|
break;
|
2016-08-21 13:29:42 +00:00
|
|
|
case R.id.modules:
|
|
|
|
setTitle(R.string.modules);
|
2016-08-23 16:28:27 +00:00
|
|
|
tag = "modules";
|
2016-08-21 13:29:42 +00:00
|
|
|
navFragment = new ModulesFragment();
|
|
|
|
break;
|
|
|
|
case R.id.log:
|
|
|
|
setTitle(R.string.log);
|
2016-08-23 16:28:27 +00:00
|
|
|
tag = "log";
|
2016-08-21 13:29:42 +00:00
|
|
|
navFragment = new LogFragment();
|
|
|
|
break;
|
2016-08-23 15:02:32 +00:00
|
|
|
case R.id.app_about:
|
|
|
|
startActivity(new Intent(this, AboutActivity.class));
|
|
|
|
return;
|
2016-08-21 13:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (navFragment != null) {
|
|
|
|
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
|
|
|
transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
|
|
|
|
try {
|
2016-08-21 14:42:50 +00:00
|
|
|
toolbar.setElevation(navFragment instanceof ModulesFragment ? 0 : 10);
|
|
|
|
|
2016-08-23 16:28:27 +00:00
|
|
|
transaction.replace(R.id.content_frame, navFragment, tag).commit();
|
2016-08-21 13:29:42 +00:00
|
|
|
} catch (IllegalStateException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 12:36:06 +00:00
|
|
|
|
2016-08-25 15:39:09 +00:00
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
|
|
|
if (requestCode == 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
new DownloadFile(this, mLastLink, mLastIsApp).execute();
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 12:36:06 +00:00
|
|
|
private class CheckUpdates extends AsyncTask<Void, Void, String> {
|
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
public CheckUpdates(Context context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
2016-08-25 12:36:06 +00:00
|
|
|
@Override
|
|
|
|
protected String doInBackground(Void... voids) {
|
|
|
|
try {
|
|
|
|
HttpURLConnection c = (HttpURLConnection) new URL(JSON_UPDATE_CHECK).openConnection();
|
|
|
|
c.setRequestMethod("GET");
|
|
|
|
c.setInstanceFollowRedirects(false);
|
|
|
|
c.setDoOutput(false);
|
|
|
|
c.connect();
|
|
|
|
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
sb.append(line);
|
|
|
|
}
|
|
|
|
br.close();
|
|
|
|
return sb.toString();
|
|
|
|
} catch (IOException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(String result) {
|
|
|
|
super.onPostExecute(result);
|
|
|
|
|
|
|
|
if (result == null) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
JSONObject json = new JSONObject(result);
|
|
|
|
|
|
|
|
JSONObject app = json.getJSONObject("app");
|
|
|
|
JSONObject magisk = json.getJSONObject("magisk");
|
|
|
|
|
|
|
|
String appVersionCode = app.getString("versionCode");
|
|
|
|
String appLink = app.getString("link");
|
|
|
|
String appChangelog = app.getString("changelog");
|
|
|
|
|
|
|
|
String magiskVersionCode = magisk.getString("versionCode");
|
|
|
|
String magiskLink = magisk.getString("link");
|
|
|
|
String magiskChangelog = magisk.getString("changelog");
|
|
|
|
|
|
|
|
if (Integer.parseInt(appVersionCode) > BuildConfig.VERSION_CODE) {
|
|
|
|
showUpdateDialog(true, appVersionCode, appLink, appChangelog);
|
|
|
|
}
|
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
List<String> versionSh = Shell.sh("getprop magisk.version");
|
|
|
|
|
|
|
|
String version = versionSh.isEmpty() ? "" : versionSh.get(0);
|
|
|
|
|
2016-08-25 12:36:06 +00:00
|
|
|
int versionInt = TextUtils.isEmpty(version) ? 0 : Integer.parseInt(version);
|
|
|
|
|
|
|
|
if (Integer.parseInt(magiskVersionCode) > versionInt) {
|
|
|
|
showUpdateDialog(false, magiskVersionCode, magiskLink, magiskChangelog);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isUpdateIgnored(String version) {
|
|
|
|
SharedPreferences prefs = getSharedPreferences(getPackageName() + "_preferences", MODE_PRIVATE);
|
|
|
|
return prefs.getBoolean("update_ignored_" + version, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setUpdateIgnored(String version) {
|
|
|
|
SharedPreferences prefs = getSharedPreferences(getPackageName() + "_preferences", MODE_PRIVATE);
|
|
|
|
prefs.edit().putBoolean("update_ignored_" + version, true).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showUpdateDialog(final boolean app, final String versionCode, final String link, String changelog) {
|
|
|
|
if (isUpdateIgnored(versionCode)) return;
|
|
|
|
|
|
|
|
String text = app ? getString(R.string.app_name) : getString(R.string.magisk);
|
|
|
|
String msg = getString(R.string.update_available_message, text, versionCode, changelog);
|
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
new AlertDialog.Builder(context)
|
2016-08-25 12:36:06 +00:00
|
|
|
.setTitle(R.string.update_available)
|
2016-08-25 12:59:07 +00:00
|
|
|
.setMessage(Html.fromHtml(msg))
|
2016-08-25 12:36:06 +00:00
|
|
|
.setCancelable(false)
|
|
|
|
.setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
2016-08-25 15:39:09 +00:00
|
|
|
mLastLink = link;
|
|
|
|
mLastIsApp = app;
|
|
|
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(WelcomeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|
|
|
|
&& Build.VERSION.SDK_INT >= 23) {
|
|
|
|
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new DownloadFile(context, link, app).execute();
|
2016-08-25 12:36:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(R.string.no_thanks, null)
|
|
|
|
.setNeutralButton(R.string.never_show_again, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
|
setUpdateIgnored(versionCode);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class DownloadFile extends AsyncTask<Void, Integer, Boolean> {
|
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
private final Context context;
|
2016-08-25 12:36:06 +00:00
|
|
|
private final String link;
|
|
|
|
private final File downloadFile;
|
|
|
|
private final ProgressDialog progress;
|
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
public DownloadFile(Context context, String link, boolean apk) {
|
2016-08-25 12:36:06 +00:00
|
|
|
this.link = link;
|
2016-08-25 12:59:07 +00:00
|
|
|
this.context = context;
|
2016-08-25 12:36:06 +00:00
|
|
|
|
2016-08-25 15:39:09 +00:00
|
|
|
File dir = new File(Environment.getExternalStorageDirectory() + "/Magisk");
|
|
|
|
|
|
|
|
if (!dir.exists()) dir.mkdir();
|
|
|
|
|
2016-08-25 12:36:06 +00:00
|
|
|
if (apk) {
|
2016-08-25 15:39:09 +00:00
|
|
|
downloadFile = new File(dir + "/MagiskManager.apk");
|
2016-08-25 12:36:06 +00:00
|
|
|
} else {
|
2016-08-25 15:39:09 +00:00
|
|
|
downloadFile = new File(dir + "/Magisk.zip");
|
2016-08-25 12:36:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
Toast.makeText(context, downloadFile.getPath(), Toast.LENGTH_SHORT).show();
|
|
|
|
|
|
|
|
progress = ProgressDialog.show(context, null, getString(R.string.loading), true, false);
|
2016-08-25 12:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(Void... voids) {
|
|
|
|
try {
|
|
|
|
URL u = new URL(link);
|
|
|
|
URLConnection conn = u.openConnection();
|
2016-08-25 12:59:07 +00:00
|
|
|
conn.connect();
|
|
|
|
|
|
|
|
int length = conn.getContentLength();
|
2016-08-25 12:36:06 +00:00
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
InputStream input = new BufferedInputStream(u.openStream(), 8192);
|
|
|
|
OutputStream output = new FileOutputStream(downloadFile);
|
2016-08-25 12:36:06 +00:00
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
byte data[] = new byte[1024];
|
|
|
|
long total = 0;
|
|
|
|
|
|
|
|
int count;
|
|
|
|
while ((count = input.read(data)) != -1) {
|
|
|
|
total += count;
|
|
|
|
output.write(data, 0, count);
|
|
|
|
|
|
|
|
publishProgress((int) ((total * 100) / length));
|
|
|
|
}
|
|
|
|
|
|
|
|
output.flush();
|
|
|
|
|
|
|
|
output.close();
|
|
|
|
input.close();
|
2016-08-25 12:36:06 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (IOException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 12:59:07 +00:00
|
|
|
@Override
|
|
|
|
protected void onProgressUpdate(Integer... values) {
|
|
|
|
super.onProgressUpdate(values);
|
|
|
|
|
|
|
|
progress.setMessage(getString(R.string.loading) + values[0] + "%");
|
|
|
|
}
|
|
|
|
|
2016-08-25 12:36:06 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Boolean result) {
|
|
|
|
super.onPostExecute(result);
|
|
|
|
progress.dismiss();
|
|
|
|
if (!result) {
|
2016-08-25 12:59:07 +00:00
|
|
|
Toast.makeText(context, R.string.error_download_file, Toast.LENGTH_LONG).show();
|
2016-08-25 12:36:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downloadFile.getPath().contains("apk")) {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
intent.setDataAndType(Uri.fromFile(downloadFile), "application/vnd.android.package-archive");
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
startActivity(intent);
|
|
|
|
} else {
|
2016-08-25 12:59:07 +00:00
|
|
|
Toast.makeText(context, R.string.flash_recovery, Toast.LENGTH_LONG).show();
|
2016-08-25 12:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-08-21 13:29:42 +00:00
|
|
|
}
|