2016-09-21 00:01:20 +02:00
|
|
|
package com.topjohnwu.magisk;
|
2016-08-21 15:29:42 +02:00
|
|
|
|
2016-08-21 17:21:37 +02:00
|
|
|
import android.Manifest;
|
|
|
|
import android.annotation.SuppressLint;
|
2016-09-30 12:22:43 +02:00
|
|
|
import android.app.Fragment;
|
2016-08-21 17:21:37 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.net.Uri;
|
2016-09-21 23:55:20 +02:00
|
|
|
import android.os.Build;
|
2016-08-21 17:21:37 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.design.widget.Snackbar;
|
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.HorizontalScrollView;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.ScrollView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
import com.topjohnwu.magisk.utils.Async;
|
|
|
|
import com.topjohnwu.magisk.utils.Shell;
|
2016-08-24 23:58:15 +02:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
2016-08-21 17:21:37 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
2016-08-24 23:58:15 +02:00
|
|
|
import java.io.FileWriter;
|
2016-08-21 17:21:37 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Calendar;
|
2016-08-24 23:58:15 +02:00
|
|
|
import java.util.List;
|
2016-08-21 17:21:37 +02:00
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public class LogFragment extends Fragment {
|
|
|
|
|
2016-09-27 18:36:58 +02:00
|
|
|
private static final String MAGISK_LOG = "/cache/magisk.log";
|
2016-08-21 17:21:37 +02:00
|
|
|
|
|
|
|
@BindView(R.id.txtLog) TextView txtLog;
|
|
|
|
@BindView(R.id.svLog) ScrollView svLog;
|
|
|
|
@BindView(R.id.hsvLog) HorizontalScrollView hsvLog;
|
|
|
|
|
|
|
|
@BindView(R.id.progressBar) ProgressBar progressBar;
|
|
|
|
|
|
|
|
private MenuItem mClickedMenuItem;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
View view = inflater.inflate(R.layout.log_fragment, container, false);
|
|
|
|
ButterKnife.bind(this, view);
|
|
|
|
|
|
|
|
txtLog.setTextIsSelectable(true);
|
|
|
|
|
2016-11-07 17:04:22 +01:00
|
|
|
new LogManager().read();
|
2016-08-21 17:21:37 +02:00
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2016-09-29 22:18:41 +02:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
setHasOptionsMenu(true);
|
2016-11-07 17:04:22 +01:00
|
|
|
new LogManager().read();
|
2016-09-29 22:18:41 +02:00
|
|
|
}
|
|
|
|
|
2016-08-21 17:21:37 +02:00
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
inflater.inflate(R.menu.menu_log, menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
mClickedMenuItem = item;
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.menu_refresh:
|
2016-11-07 17:04:22 +01:00
|
|
|
new LogManager().read();
|
2016-08-21 17:21:37 +02:00
|
|
|
return true;
|
|
|
|
case R.id.menu_send:
|
2016-11-07 16:59:10 +01:00
|
|
|
new LogManager().send();
|
2016-08-21 17:21:37 +02:00
|
|
|
return true;
|
|
|
|
case R.id.menu_save:
|
2016-11-07 16:59:10 +01:00
|
|
|
new LogManager().save();
|
2016-08-21 17:21:37 +02:00
|
|
|
return true;
|
|
|
|
case R.id.menu_clear:
|
2016-11-07 16:59:10 +01:00
|
|
|
new LogManager().clear();
|
|
|
|
return true;
|
|
|
|
default:
|
2016-08-21 17:21:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
if (requestCode == 0) {
|
|
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
if (mClickedMenuItem != null) {
|
2016-08-29 00:35:07 +02:00
|
|
|
new Handler().postDelayed(() -> onOptionsItemSelected(mClickedMenuItem), 500);
|
2016-08-21 17:21:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Snackbar.make(txtLog, R.string.permissionNotGranted, Snackbar.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
public class LogManager extends Async.RootTask<Object, Void, Object> {
|
2016-08-21 17:21:37 +02:00
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
int mode;
|
2016-09-29 17:24:31 +02:00
|
|
|
File targetFile;
|
2016-08-21 17:21:37 +02:00
|
|
|
|
2016-09-29 17:24:31 +02:00
|
|
|
@SuppressLint("DefaultLocale")
|
|
|
|
@Override
|
2016-11-07 16:59:10 +01:00
|
|
|
protected Object doInBackground(Object... params) {
|
|
|
|
mode = (int) params[0];
|
|
|
|
switch (mode) {
|
|
|
|
case 0:
|
|
|
|
List<String> logList = Utils.readFile(MAGISK_LOG);
|
2016-09-29 17:24:31 +02:00
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
StringBuilder llog = new StringBuilder(15 * 10 * 1024);
|
|
|
|
for (String s : logList) {
|
|
|
|
llog.append(s).append("\n");
|
|
|
|
}
|
2016-08-21 17:21:37 +02:00
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
return llog.toString();
|
2016-09-29 17:24:31 +02:00
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
case 1:
|
|
|
|
Shell.su("echo > " + MAGISK_LOG);
|
|
|
|
Snackbar.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
|
|
|
|
return "";
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Calendar now = Calendar.getInstance();
|
|
|
|
String filename = String.format(
|
|
|
|
"magisk_%s_%04d%02d%02d_%02d%02d%02d.log", "error",
|
|
|
|
now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1,
|
|
|
|
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
|
|
|
|
now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
|
|
|
|
|
|
|
|
targetFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MagiskManager/" + filename);
|
|
|
|
|
|
|
|
if ((!targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs())
|
|
|
|
|| (targetFile.exists() && !targetFile.delete()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
List<String> in = Utils.readFile(MAGISK_LOG);
|
|
|
|
|
|
|
|
try {
|
|
|
|
FileWriter out = new FileWriter(targetFile);
|
|
|
|
for (String line : in) {
|
|
|
|
out.write(line + "\n");
|
|
|
|
}
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-21 17:21:37 +02:00
|
|
|
}
|
2016-11-07 16:59:10 +01:00
|
|
|
return null;
|
|
|
|
}
|
2016-08-21 17:21:37 +02:00
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Object o) {
|
|
|
|
boolean bool;
|
|
|
|
String llog;
|
|
|
|
switch (mode) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
llog = (String) o;
|
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
if (llog.length() == 0)
|
|
|
|
txtLog.setText(R.string.log_is_empty);
|
|
|
|
else
|
|
|
|
txtLog.setText(llog);
|
2016-11-07 17:04:22 +01:00
|
|
|
svLog.post(() -> svLog.scrollTo(0, txtLog.getHeight()));
|
|
|
|
hsvLog.post(() -> hsvLog.scrollTo(0, 0));
|
2016-11-07 16:59:10 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
bool = (boolean) o;
|
|
|
|
if (bool)
|
|
|
|
Toast.makeText(getActivity(), targetFile.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
else
|
|
|
|
Toast.makeText(getActivity(), getString(R.string.logs_save_failed), Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
bool = (boolean) o;
|
|
|
|
if (bool) {
|
|
|
|
Intent sendIntent = new Intent();
|
|
|
|
sendIntent.setAction(Intent.ACTION_SEND);
|
|
|
|
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(targetFile));
|
|
|
|
sendIntent.setType("application/html");
|
|
|
|
startActivity(Intent.createChooser(sendIntent, getResources().getString(R.string.menuSend)));
|
|
|
|
} else {
|
|
|
|
Toast.makeText(getActivity(), getString(R.string.logs_save_failed), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2016-09-29 17:24:31 +02:00
|
|
|
}
|
2016-08-21 17:21:37 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
public void read() {
|
|
|
|
exec(0);
|
2016-08-21 17:21:37 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
public void clear() {
|
|
|
|
exec(1);
|
2016-08-21 17:21:37 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
public void save() {
|
|
|
|
exec(2);
|
2016-08-21 17:21:37 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 16:59:10 +01:00
|
|
|
public void send() {
|
|
|
|
exec(3);
|
2016-08-21 17:21:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 15:29:42 +02:00
|
|
|
}
|