2016-08-21 13:29:42 +00:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
2016-08-27 19:52:03 +00:00
|
|
|
import android.Manifest;
|
2016-09-21 21:55:20 +00:00
|
|
|
import android.app.Fragment;
|
2016-09-23 21:22:11 +00:00
|
|
|
import android.app.FragmentTransaction;
|
2016-08-23 15:02:32 +00:00
|
|
|
import android.content.Intent;
|
2016-12-25 14:36:51 +00:00
|
|
|
import android.content.SharedPreferences;
|
2016-08-27 19:52:03 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2016-08-21 15:42:42 +00:00
|
|
|
import android.os.Build;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
2016-09-25 05:16:28 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.design.widget.NavigationView;
|
2016-08-27 19:52:03 +00:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
2016-08-21 13:29:42 +00: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;
|
2016-10-22 18:04:58 +00:00
|
|
|
import android.view.Menu;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
2016-08-24 21:58:15 +00:00
|
|
|
|
2016-12-25 14:36:51 +00:00
|
|
|
import com.topjohnwu.magisk.utils.CallbackHandler;
|
2016-12-27 06:30:26 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Shell;
|
2017-01-04 22:11:09 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
2016-08-27 19:52:03 +00:00
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
2016-12-25 14:36:51 +00:00
|
|
|
public class MainActivity extends AppCompatActivity
|
|
|
|
implements NavigationView.OnNavigationItemSelectedListener, CallbackHandler.EventListener {
|
2016-08-21 13:29:42 +00:00
|
|
|
|
|
|
|
private static final String SELECTED_ITEM_ID = "SELECTED_ITEM_ID";
|
2016-08-25 12:59:07 +00:00
|
|
|
|
2017-01-11 23:41:09 +00:00
|
|
|
public static final CallbackHandler.Event recreate = new CallbackHandler.Event();
|
2017-01-11 11:10:30 +00:00
|
|
|
|
2016-08-23 09:39:18 +00:00
|
|
|
private final Handler mDrawerHandler = new Handler();
|
2016-12-25 14:36:51 +00:00
|
|
|
private SharedPreferences prefs;
|
2016-08-25 09:04:56 +00:00
|
|
|
|
2016-09-29 15:24:31 +00:00
|
|
|
@BindView(R.id.toolbar) Toolbar toolbar;
|
|
|
|
@BindView(R.id.drawer_layout) DrawerLayout drawer;
|
2016-12-24 19:05:22 +00:00
|
|
|
@BindView(R.id.nav_view) public NavigationView navigationView;
|
2016-08-21 13:29:42 +00:00
|
|
|
|
2016-12-24 19:05:22 +00:00
|
|
|
private int mSelectedId = R.id.status;
|
2016-08-22 21:18:28 +00:00
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
2016-09-25 05:16:28 +00:00
|
|
|
|
2016-12-25 14:36:51 +00:00
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
|
|
|
|
2017-01-10 14:30:05 +00:00
|
|
|
if (Utils.isDarkTheme) {
|
2016-09-25 05:16:28 +00:00
|
|
|
setTheme(R.style.AppTheme_dh);
|
|
|
|
}
|
2016-08-21 13:29:42 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
2016-09-24 16:54:12 +00:00
|
|
|
setContentView(R.layout.activity_main);
|
2016-08-21 13:29:42 +00:00
|
|
|
ButterKnife.bind(this);
|
2016-08-23 16:28:27 +00:00
|
|
|
|
2016-08-28 22:35:07 +00: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-09-19 06:46:07 +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);
|
2016-09-19 21:48:13 +00:00
|
|
|
super.onDrawerSlide(drawerView, 0); // this disables the arrow @ completed tate
|
2016-08-21 13:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-09-29 20:18:41 +00:00
|
|
|
if (savedInstanceState == null) {
|
2017-01-12 00:32:16 +00:00
|
|
|
navigate(mSelectedId, true);
|
|
|
|
navigationView.setCheckedItem(mSelectedId);
|
|
|
|
} else {
|
|
|
|
mSelectedId = savedInstanceState.getInt(SELECTED_ITEM_ID);
|
2016-09-29 20:18:41 +00:00
|
|
|
}
|
2016-08-21 13:29:42 +00:00
|
|
|
|
2016-08-21 15:21:37 +00:00
|
|
|
navigationView.setNavigationItemSelectedListener(this);
|
2017-01-11 23:51:44 +00:00
|
|
|
CallbackHandler.register(recreate, this);
|
2016-08-31 21:52:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 18:04:58 +00:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
2016-12-25 14:36:51 +00:00
|
|
|
CallbackHandler.register(StatusFragment.updateCheckDone, this);
|
2017-01-11 11:10:30 +00:00
|
|
|
if (StatusFragment.updateCheckDone.isTriggered) {
|
|
|
|
onTrigger(StatusFragment.updateCheckDone);
|
|
|
|
}
|
2016-10-22 18:04:58 +00:00
|
|
|
checkHideSection();
|
|
|
|
}
|
|
|
|
|
2017-01-11 11:10:30 +00:00
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
CallbackHandler.unRegister(StatusFragment.updateCheckDone, this);
|
2017-01-11 23:51:44 +00:00
|
|
|
super.onPause();
|
2017-01-11 11:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 14:36:51 +00:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2017-01-11 11:10:30 +00:00
|
|
|
CallbackHandler.unRegister(recreate, this);
|
2017-01-11 23:51:44 +00:00
|
|
|
super.onDestroy();
|
2016-12-25 14:36:51 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-09-30 03:35:46 +00:00
|
|
|
finish();
|
2016-08-21 13:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2017-01-12 00:32:16 +00:00
|
|
|
mDrawerHandler.postDelayed(() -> navigate(menuItem.getItemId(), false), 250);
|
2016-08-21 13:29:42 +00:00
|
|
|
drawer.closeDrawer(GravityCompat.START);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-25 14:36:51 +00:00
|
|
|
@Override
|
|
|
|
public void onTrigger(CallbackHandler.Event event) {
|
2017-01-11 11:10:30 +00:00
|
|
|
if (event == StatusFragment.updateCheckDone) {
|
|
|
|
Menu menu = navigationView.getMenu();
|
|
|
|
menu.findItem(R.id.install).setVisible(StatusFragment.remoteMagiskVersion > 0 &&
|
|
|
|
Shell.rootAccess());
|
|
|
|
} else if (event == recreate) {
|
|
|
|
recreate();
|
|
|
|
}
|
2016-12-25 14:36:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 18:04:58 +00:00
|
|
|
private void checkHideSection() {
|
|
|
|
Menu menu = navigationView.getMenu();
|
2017-01-11 11:10:30 +00:00
|
|
|
menu.findItem(R.id.magiskhide).setVisible(StatusFragment.magiskVersion >= 8 &&
|
2016-12-27 06:30:26 +00:00
|
|
|
prefs.getBoolean("magiskhide", false) && Shell.rootAccess());
|
2017-01-11 11:10:30 +00:00
|
|
|
menu.findItem(R.id.modules).setVisible(StatusFragment.magiskVersion >= 4 &&
|
2017-01-10 14:47:58 +00:00
|
|
|
Shell.rootAccess());
|
2017-01-11 11:10:30 +00:00
|
|
|
menu.findItem(R.id.downloads).setVisible(StatusFragment.magiskVersion >= 4 &&
|
2017-01-10 14:47:58 +00:00
|
|
|
Shell.rootAccess());
|
|
|
|
menu.findItem(R.id.log).setVisible(Shell.rootAccess());
|
|
|
|
menu.findItem(R.id.install).setVisible(Shell.rootAccess());
|
2016-10-22 18:04:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 00:32:16 +00:00
|
|
|
public void navigate(int itemId, boolean now) {
|
2016-08-21 13:29:42 +00:00
|
|
|
switch (itemId) {
|
2016-12-24 19:05:22 +00:00
|
|
|
case R.id.status:
|
2017-01-12 00:32:16 +00:00
|
|
|
displayFragment(new StatusFragment(), "status", now);
|
2016-12-24 19:05:22 +00:00
|
|
|
break;
|
|
|
|
case R.id.install:
|
2017-01-12 00:32:16 +00:00
|
|
|
displayFragment(new InstallFragment(), "install", now);
|
2016-08-23 09:39:18 +00:00
|
|
|
break;
|
2016-08-21 13:29:42 +00:00
|
|
|
case R.id.modules:
|
2017-01-12 00:32:16 +00:00
|
|
|
displayFragment(new ModulesFragment(), "modules", now);
|
2016-08-21 13:29:42 +00:00
|
|
|
break;
|
2016-09-11 18:36:58 +00:00
|
|
|
case R.id.downloads:
|
2017-01-12 00:32:16 +00:00
|
|
|
displayFragment(new ReposFragment(), "downloads", now);
|
2016-09-11 18:36:58 +00:00
|
|
|
break;
|
2016-10-22 18:04:58 +00:00
|
|
|
case R.id.magiskhide:
|
2017-01-12 00:32:16 +00:00
|
|
|
displayFragment(new MagiskHideFragment(), "magiskhide", now);
|
2016-10-22 18:04:58 +00:00
|
|
|
break;
|
2016-08-21 13:29:42 +00:00
|
|
|
case R.id.log:
|
2017-01-12 00:32:16 +00:00
|
|
|
displayFragment(new LogFragment(), "log", now);
|
2016-08-21 13:29:42 +00:00
|
|
|
break;
|
2016-09-21 21:55:20 +00:00
|
|
|
case R.id.settings:
|
2016-09-29 20:18:41 +00:00
|
|
|
startActivity(new Intent(this, SettingsActivity.class));
|
2016-09-21 21:55:20 +00:00
|
|
|
break;
|
2016-08-23 15:02:32 +00:00
|
|
|
case R.id.app_about:
|
|
|
|
startActivity(new Intent(this, AboutActivity.class));
|
2017-01-12 00:32:16 +00:00
|
|
|
break;
|
2016-08-21 13:29:42 +00:00
|
|
|
}
|
2017-01-12 00:32:16 +00:00
|
|
|
}
|
2016-08-21 13:29:42 +00:00
|
|
|
|
2017-01-12 00:32:16 +00:00
|
|
|
private void displayFragment(@NonNull Fragment navFragment, String tag, boolean now) {
|
|
|
|
FragmentTransaction transaction = getFragmentManager().beginTransaction();
|
|
|
|
if (!now) {
|
2016-09-21 21:55:20 +00:00
|
|
|
transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
|
2016-08-21 13:29:42 +00:00
|
|
|
}
|
2017-01-12 00:32:16 +00:00
|
|
|
transaction.replace(R.id.content_frame, navFragment, tag).commit();
|
2016-08-21 13:29:42 +00:00
|
|
|
}
|
2017-01-12 00:32:16 +00:00
|
|
|
}
|