Magisk/app/src/main/java/com/topjohnwu/magisk/MainActivity.java

228 lines
8.0 KiB
Java
Raw Normal View History

2016-08-21 15:29:42 +02:00
package com.topjohnwu.magisk;
2016-08-27 21:52:03 +02:00
import android.Manifest;
2016-08-23 17:02:32 +02:00
import android.content.Intent;
2016-12-25 15:36:51 +01:00
import android.content.SharedPreferences;
2016-08-27 21:52:03 +02:00
import android.content.pm.PackageManager;
2016-08-21 17:42:42 +02:00
import android.os.Build;
2016-08-21 15:29:42 +02:00
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
2016-08-27 21:52:03 +02:00
import android.support.v4.app.ActivityCompat;
2017-01-28 17:20:43 +01:00
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
2016-08-21 15:29:42 +02:00
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
2016-10-22 20:04:58 +02:00
import android.view.Menu;
2016-08-21 15:29:42 +02:00
import android.view.MenuItem;
import android.view.View;
2017-02-06 19:01:32 +01:00
import com.topjohnwu.magisk.components.Activity;
2017-02-06 21:09:49 +01:00
import com.topjohnwu.magisk.utils.CallbackEvent;
2016-12-27 07:30:26 +01:00
import com.topjohnwu.magisk.utils.Shell;
2016-08-27 21:52:03 +02:00
2016-08-21 15:29:42 +02:00
import butterknife.BindView;
import butterknife.ButterKnife;
2017-02-06 19:01:32 +01:00
public class MainActivity extends Activity
2017-02-06 21:09:49 +01:00
implements NavigationView.OnNavigationItemSelectedListener, CallbackEvent.Listener<Void> {
2016-08-21 15:29:42 +02:00
2016-08-23 11:39:18 +02:00
private final Handler mDrawerHandler = new Handler();
2016-12-25 15:36:51 +01:00
private SharedPreferences prefs;
2017-02-17 01:51:51 +01:00
private int mDrawerItem;
2016-08-25 11:04:56 +02:00
2016-09-29 17:24:31 +02:00
@BindView(R.id.toolbar) Toolbar toolbar;
@BindView(R.id.drawer_layout) DrawerLayout drawer;
2016-12-24 20:05:22 +01:00
@BindView(R.id.nav_view) public NavigationView navigationView;
2016-08-21 15:29:42 +02:00
2017-01-27 18:10:50 +01:00
private float toolbarElevation;
2016-08-22 23:18:28 +02:00
2016-08-21 15:29:42 +02:00
@Override
protected void onCreate(final Bundle savedInstanceState) {
2017-02-17 01:51:51 +01:00
prefs = getApplicationContext().prefs;
2016-12-25 15:36:51 +01:00
2017-02-17 01:51:51 +01:00
if (getApplicationContext().isDarkTheme) {
2017-02-14 09:35:03 +01:00
setTheme(R.style.AppTheme_Dark);
}
2016-08-21 15:29:42 +02:00
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
2016-08-21 15:29:42 +02:00
ButterKnife.bind(this);
2016-08-29 00:35:07 +02:00
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
2016-08-21 15:29:42 +02: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 tate
2016-08-21 15:29:42 +02:00
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, 0); // this disables the animation
}
};
2017-01-27 18:10:50 +01:00
toolbarElevation = toolbar.getElevation();
2016-08-21 17:21:37 +02:00
drawer.addDrawerListener(toggle);
2016-08-21 15:29:42 +02:00
toggle.syncState();
2017-02-17 07:07:15 +01:00
if (savedInstanceState == null)
navigate(getIntent().getStringExtra(MagiskManager.INTENT_SECTION));
2016-08-21 15:29:42 +02:00
2016-08-21 17:21:37 +02:00
navigationView.setNavigationItemSelectedListener(this);
2017-02-17 01:51:51 +01:00
getApplicationContext().reloadMainActivity.register(this);
2017-03-29 23:16:50 +02:00
getApplicationContext().updateCheckDone.register(this);
2017-02-14 09:35:03 +01:00
}
2016-10-22 20:04:58 +02:00
@Override
protected void onResume() {
super.onResume();
checkHideSection();
}
2017-02-17 07:07:15 +01:00
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
2017-05-21 06:16:38 +02:00
navigate(savedInstanceState.getInt(MagiskManager.INTENT_SECTION, R.id.magisk));
2017-02-17 07:07:15 +01:00
}
2017-01-11 12:10:30 +01:00
@Override
2017-02-17 01:51:51 +01:00
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(MagiskManager.INTENT_SECTION, mDrawerItem);
2017-01-11 12:10:30 +01:00
}
2016-12-25 15:36:51 +01:00
@Override
protected void onDestroy() {
2017-02-17 01:51:51 +01:00
getApplicationContext().reloadMainActivity.unRegister(this);
2017-03-29 23:16:50 +02:00
getApplicationContext().updateCheckDone.unRegister(this);
super.onDestroy();
2016-12-25 15:36:51 +01:00
}
2016-08-21 15:29:42 +02:00
@Override
public void onBackPressed() {
2017-01-30 13:04:49 +01:00
if (drawer.isDrawerOpen(navigationView))
drawer.closeDrawer(navigationView);
2017-01-30 13:04:49 +01:00
else
2016-09-30 05:35:46 +02:00
finish();
2016-08-21 15:29:42 +02:00
}
@Override
public boolean onNavigationItemSelected(@NonNull final MenuItem menuItem) {
mDrawerHandler.removeCallbacksAndMessages(null);
2017-01-28 17:20:43 +01:00
mDrawerHandler.postDelayed(() -> navigate(menuItem.getItemId()), 250);
drawer.closeDrawer(navigationView);
2016-08-21 15:29:42 +02:00
return true;
}
2016-12-25 15:36:51 +01:00
@Override
2017-02-06 21:09:49 +01:00
public void onTrigger(CallbackEvent<Void> event) {
2017-03-29 23:16:50 +02:00
if (event == getApplicationContext().reloadMainActivity) {
recreate();
} else if (event == getApplicationContext().updateCheckDone) {
checkHideSection();
}
2016-12-25 15:36:51 +01:00
}
2016-10-22 20:04:58 +02:00
private void checkHideSection() {
Menu menu = navigationView.getMenu();
menu.findItem(R.id.magiskhide).setVisible(
2017-05-11 20:25:07 +02:00
Shell.rootAccess() && getApplicationContext().magiskVersionCode >= 130
&& prefs.getBoolean("magiskhide", false));
menu.findItem(R.id.modules).setVisible(
2017-05-11 20:25:07 +02:00
Shell.rootAccess() && getApplicationContext().magiskVersionCode >= 0);
menu.findItem(R.id.downloads).setVisible(
2017-05-11 20:25:07 +02:00
Shell.rootAccess() && getApplicationContext().magiskVersionCode >= 0);
menu.findItem(R.id.log).setVisible(Shell.rootAccess());
menu.findItem(R.id.superuser).setVisible(
Shell.rootAccess() && getApplicationContext().isSuClient);
2017-02-17 01:51:51 +01:00
}
public void navigate(String item) {
2017-05-21 06:16:38 +02:00
int itemId = R.id.magisk;
2017-02-17 01:51:51 +01:00
if (item != null) {
switch (item) {
2017-05-21 06:16:38 +02:00
case "magisk":
itemId = R.id.magisk;
2017-02-17 01:51:51 +01:00
break;
case "superuser":
itemId = R.id.superuser;
break;
case "modules":
itemId = R.id.modules;
break;
case "downloads":
itemId = R.id.downloads;
break;
case "magiskhide":
itemId = R.id.magiskhide;
break;
case "log":
itemId = R.id.log;
break;
case "settings":
itemId = R.id.settings;
break;
case "about":
itemId = R.id.app_about;
break;
}
2017-01-26 20:38:53 +01:00
}
2017-02-17 01:51:51 +01:00
navigate(itemId);
2016-10-22 20:04:58 +02:00
}
2017-01-28 17:20:43 +01:00
public void navigate(int itemId) {
int bak = mDrawerItem;
2017-02-17 01:51:51 +01:00
mDrawerItem = itemId;
navigationView.setCheckedItem(itemId);
2016-08-21 15:29:42 +02:00
switch (itemId) {
2017-05-21 06:16:38 +02:00
case R.id.magisk:
displayFragment(new MagiskFragment(), "magisk", true);
2016-08-23 11:39:18 +02:00
break;
2017-01-25 18:13:23 +01:00
case R.id.superuser:
2017-01-28 17:20:43 +01:00
displayFragment(new SuperuserFragment(), "superuser", true);
2017-01-25 18:13:23 +01:00
break;
2016-08-21 15:29:42 +02:00
case R.id.modules:
2017-01-28 17:20:43 +01:00
displayFragment(new ModulesFragment(), "modules", true);
2016-08-21 15:29:42 +02:00
break;
case R.id.downloads:
2017-01-28 17:20:43 +01:00
displayFragment(new ReposFragment(), "downloads", true);
break;
2016-10-22 20:04:58 +02:00
case R.id.magiskhide:
2017-01-28 17:20:43 +01:00
displayFragment(new MagiskHideFragment(), "magiskhide", true);
2016-10-22 20:04:58 +02:00
break;
2016-08-21 15:29:42 +02:00
case R.id.log:
2017-01-28 17:20:43 +01:00
displayFragment(new LogFragment(), "log", false);
2016-08-21 15:29:42 +02:00
break;
2016-09-21 23:55:20 +02:00
case R.id.settings:
startActivity(new Intent(this, SettingsActivity.class));
mDrawerItem = bak;
2016-09-21 23:55:20 +02:00
break;
2016-08-23 17:02:32 +02:00
case R.id.app_about:
startActivity(new Intent(this, AboutActivity.class));
mDrawerItem = bak;
break;
2016-08-21 15:29:42 +02:00
}
}
2016-08-21 15:29:42 +02:00
2017-01-28 17:20:43 +01:00
private void displayFragment(@NonNull Fragment navFragment, String tag, boolean setElevation) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
supportInvalidateOptionsMenu();
2017-01-28 17:20:43 +01:00
transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
transaction.replace(R.id.content_frame, navFragment, tag).commitNow();
if (setElevation) toolbar.setElevation(toolbarElevation);
2017-02-17 01:51:51 +01:00
else toolbar.setElevation(0);
2016-08-21 15:29:42 +02:00
}
}