/*
* DroidFix Project
* file MainActivity.java is part of DroidFix
* The MIT License (MIT) Copyright (c) 2015 Bunny Blue.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package io.github.bunnyblue.droidfix;
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.*;
import android.os.Process;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import io.github.bunnyblue.droidfix.dexloader.DroidFix;
import io.github.bunnyblue.droidfix.dexloader.ZipUtil;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (PackageManager.PERMISSION_DENIED==ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE))
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},123);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (PackageManager.PERMISSION_DENIED==ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE))
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},123);
}
} else if (id == R.id.nav_share) {
startActivity(new Intent(MainActivity.this,BugActivity.class));
Toast.makeText(MainActivity.this,"fixdebug" ,Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_send) {
File file =new File(Environment.getExternalStorageDirectory(),"patch.apk");
if (!file.exists()){
Toast.makeText(this, "file not found", Toast.LENGTH_SHORT).show();
return true;
}
final File sourceApk = new File(getApplicationInfo().sourceDir);
// File dest=new File(getFilesDir(), DroidFix.DROID_CODE_CACHE);
File dest= null;
try {
//dest = ZipUtil.copyAsset(this, "patch.apk", new File(getFilesDir(), DroidFix.DROID_CODE_CACHE));
dest = ZipUtil.copyFile(file,new File(getFilesDir(), DroidFix.DROID_CODE_CACHE));
} catch (IOException e) {
e.printStackTrace();
}
// try {
// ZipFile zipFile=new ZipFile(sourceApk);
// MultiDexExtractor.extract(zipFile, zipFile.getEntry("assets/patch.dex"),dest);
// } catch (IOException e) {
// e.printStackTrace();
// }
// dest.getParentFile().mkdirs();
// try {
// DroidFix.copyFile(file,dest);
// } catch (IOException e) {
// e.printStackTrace();
// }
Toast.makeText(this, "补丁安装完毕,需要重启app", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("安装安全补丁").setMessage("点击确定重启App").setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();;
android.os.Process.killProcess(Process.myPid());
}
});
builder.create().show();;
DroidFix.installPatch(this, dest);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}