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

268 lines
9.1 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-09-21 23:55:20 +02:00
import android.app.Fragment;
2016-09-23 23:22:11 +02:00
import android.app.FragmentTransaction;
2016-08-23 17:02:32 +02:00
import android.content.Intent;
2016-08-27 21:52:03 +02:00
import android.content.pm.PackageManager;
2016-09-24 20:46:42 +02:00
import android.graphics.Color;
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;
2016-08-21 17:21:37 +02:00
import android.support.annotation.IdRes;
2016-08-21 15:29:42 +02:00
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;
2016-08-21 15:29:42 +02:00
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
2016-09-24 20:46:42 +02:00
import android.widget.ImageButton;
2016-09-23 23:22:11 +02:00
import com.topjohnwu.magisk.utils.Logger;
2016-08-29 00:35:07 +02:00
import com.topjohnwu.magisk.utils.Utils;
2016-08-27 21:52:03 +02:00
2016-09-24 20:46:42 +02:00
2016-08-21 15:29:42 +02:00
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
2016-08-21 15:29:42 +02:00
private static final String SELECTED_ITEM_ID = "SELECTED_ITEM_ID";
2016-08-23 11:39:18 +02:00
private final Handler mDrawerHandler = new Handler();
2016-09-23 23:22:11 +02:00
private String currentTitle;
2016-09-24 20:46:42 +02:00
private View mView;
2016-08-25 11:04:56 +02:00
2016-09-16 06:46:10 +02: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:29:42 +02:00
2016-08-21 17:21:37 +02:00
@IdRes
2016-08-23 11:39:18 +02:00
private int mSelectedId = R.id.magisk;
2016-08-22 23:18:28 +02:00
2016-08-21 15:29:42 +02:00
@Override
protected void onCreate(final Bundle savedInstanceState) {
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
// Startups
Utils.init(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);
}
this.getFragmentManager().addOnBackStackChangedListener(
() -> {
2016-09-24 17:07:30 +02:00
Fragment hm = getFragmentManager().findFragmentByTag("root");
if (hm != null) {
if (hm.isVisible()) {
navigationView.setCheckedItem(R.id.root);
}
}
2016-09-24 17:07:30 +02:00
hm = getFragmentManager().findFragmentByTag("autoroot");
if (hm != null) {
if (hm.isVisible()) {
navigationView.setCheckedItem(R.id.autoroot);
}
}
2016-09-24 17:07:30 +02:00
hm = getFragmentManager().findFragmentByTag("magisk");
if (hm != null) {
if (hm.isVisible()) {
navigationView.setCheckedItem(R.id.magisk);
}
}
2016-09-24 17:07:30 +02:00
hm = getFragmentManager().findFragmentByTag("modules");
if (hm != null) {
if (hm.isVisible()) {
navigationView.setCheckedItem(R.id.modules);
}
}
2016-09-24 17:07:30 +02:00
hm = getFragmentManager().findFragmentByTag("downloads");
if (hm != null) {
if (hm.isVisible()) {
navigationView.setCheckedItem(R.id.downloads);
}
}
2016-09-24 17:07:30 +02:00
hm = getFragmentManager().findFragmentByTag("log");
if (hm != null) {
if (hm.isVisible()) {
navigationView.setCheckedItem(R.id.log);
}
}
2016-09-24 17:07:30 +02:00
hm = getFragmentManager().findFragmentByTag("settings");
if (hm != null) {
if (hm.isVisible()) {
navigationView.setCheckedItem(R.id.settings);
}
}
2016-09-24 20:46:42 +02:00
}
);
2016-09-15 23:59:34 +02:00
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
}
};
2016-08-21 17:21:37 +02:00
drawer.addDrawerListener(toggle);
2016-08-21 15:29:42 +02:00
toggle.syncState();
2016-08-21 17:21:37 +02:00
//noinspection ResourceType
mSelectedId = savedInstanceState == null ? mSelectedId : savedInstanceState.getInt(SELECTED_ITEM_ID);
navigationView.setCheckedItem(mSelectedId);
2016-08-21 15:29:42 +02:00
2016-08-21 17:21:37 +02:00
if (savedInstanceState == null) {
2016-08-21 15:29:42 +02:00
mDrawerHandler.removeCallbacksAndMessages(null);
2016-08-27 13:02:41 +02:00
mDrawerHandler.postDelayed(() -> navigate(mSelectedId), 250);
2016-08-21 15:29:42 +02:00
}
2016-08-21 17:21:37 +02:00
navigationView.setNavigationItemSelectedListener(this);
2016-09-24 20:46:42 +02:00
mView = getToolbarNavigationButton();
2016-09-15 23:59:34 +02:00
if (getIntent().hasExtra("relaunch")) {
navigate(R.id.root);
}
2016-09-24 20:46:42 +02:00
startTour();
}
public ImageButton getToolbarNavigationButton() {
int size = toolbar.getChildCount();
for (int i = 0; i < size; i++) {
View child = toolbar.getChildAt(i);
if (child instanceof ImageButton) {
ImageButton btn = (ImageButton) child;
if (btn.getDrawable() == toolbar.getNavigationIcon()) {
return btn;
}
}
}
return null;
}
2016-09-22 06:36:28 +02:00
@Override
protected void onDestroy() {
super.onDestroy();
}
2016-09-24 20:46:42 +02:00
private void startTour() {
navigate(R.id.root);
}
2016-08-21 17:21:37 +02:00
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SELECTED_ITEM_ID, mSelectedId);
2016-08-21 15:29:42 +02:00
}
@Override
public void onBackPressed() {
2016-09-23 23:22:11 +02:00
2016-08-21 15:29:42 +02:00
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
2016-09-23 23:22:11 +02:00
int backStackEntryCount = getFragmentManager().getBackStackEntryCount();
Logger.dh("Welcomeactivity: Entrycount is " + backStackEntryCount);
2016-09-24 17:07:30 +02:00
if (backStackEntryCount >= 2) {
2016-09-23 23:22:11 +02:00
super.onBackPressed();
} else {
finish();
}
2016-08-21 15:29:42 +02:00
}
2016-09-23 23:22:11 +02:00
2016-08-21 15:29:42 +02:00
}
@Override
public boolean onNavigationItemSelected(@NonNull final MenuItem menuItem) {
2016-08-21 17:21:37 +02:00
mSelectedId = menuItem.getItemId();
2016-08-21 15:29:42 +02:00
mDrawerHandler.removeCallbacksAndMessages(null);
2016-08-27 13:02:41 +02:00
mDrawerHandler.postDelayed(() -> navigate(menuItem.getItemId()), 250);
2016-08-21 15:29:42 +02:00
drawer.closeDrawer(GravityCompat.START);
return true;
}
2016-09-23 23:22:11 +02:00
@Override
protected void onResume() {
super.onResume();
setTitle(currentTitle);
}
2016-09-15 23:59:34 +02:00
public void navigate(final int itemId) {
2016-08-21 15:29:42 +02:00
Fragment navFragment = null;
2016-09-24 17:07:30 +02:00
String tag = "";
2016-08-21 15:29:42 +02:00
switch (itemId) {
2016-08-23 11:39:18 +02:00
case R.id.magisk:
setTitle(R.string.magisk);
tag = "magisk";
2016-08-23 11:39:18 +02:00
navFragment = new MagiskFragment();
break;
2016-08-26 12:45:35 +02:00
case R.id.root:
setTitle(R.string.root);
tag = "root";
navFragment = new RootFragment();
break;
2016-09-15 23:59:34 +02:00
case R.id.autoroot:
setTitle(R.string.auto_toggle_apps);
tag = "autoroot";
2016-09-15 23:59:34 +02:00
navFragment = new AutoRootFragment();
break;
2016-08-21 15:29:42 +02:00
case R.id.modules:
setTitle(R.string.modules);
tag = "modules";
2016-08-21 15:29:42 +02:00
navFragment = new ModulesFragment();
break;
case R.id.downloads:
setTitle(R.string.downloads);
tag = "downloads";
navFragment = new ReposFragment();
break;
2016-08-21 15:29:42 +02:00
case R.id.log:
setTitle(R.string.log);
tag = "log";
2016-08-21 15:29:42 +02:00
navFragment = new LogFragment();
break;
2016-09-21 23:55:20 +02:00
case R.id.settings:
setTitle(R.string.settings);
tag = "settings";
navFragment = new SettingsFragment();
break;
2016-08-23 17:02:32 +02:00
case R.id.app_about:
startActivity(new Intent(this, AboutActivity.class));
return;
2016-08-21 15:29:42 +02:00
}
if (navFragment != null) {
2016-09-23 23:22:11 +02:00
2016-09-21 23:55:20 +02:00
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
2016-08-21 15:29:42 +02:00
try {
toolbar.setElevation(navFragment instanceof ModulesFragment ? 0 : 10);
2016-09-23 23:22:11 +02:00
currentTitle = getTitle().toString();
2016-09-23 23:22:11 +02:00
transaction.replace(R.id.content_frame, navFragment, tag).addToBackStack(currentTitle).commit();
2016-08-21 15:29:42 +02:00
} catch (IllegalStateException ignored) {
}
}
}
}