2017-01-26 19:38:53 +00:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.design.widget.Snackbar;
|
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
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;
|
|
|
|
|
2017-02-12 11:49:46 +00:00
|
|
|
import com.topjohnwu.magisk.asyncs.SerialTask;
|
2017-02-06 18:01:32 +00:00
|
|
|
import com.topjohnwu.magisk.components.Fragment;
|
2017-02-14 21:24:02 +00:00
|
|
|
import com.topjohnwu.magisk.components.SnackbarMaker;
|
2017-01-26 19:38:53 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Shell;
|
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
import butterknife.Unbinder;
|
|
|
|
|
|
|
|
public class MagiskLogFragment extends Fragment {
|
|
|
|
|
|
|
|
private static final String MAGISK_LOG = "/cache/magisk.log";
|
|
|
|
|
|
|
|
private Unbinder unbinder;
|
|
|
|
@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 void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
View view = inflater.inflate(R.layout.fragment_magisk_log, container, false);
|
|
|
|
unbinder = ButterKnife.bind(this, view);
|
|
|
|
|
|
|
|
txtLog.setTextIsSelectable(true);
|
|
|
|
|
|
|
|
new LogManager().read();
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
getActivity().setTitle(R.string.log);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
new LogManager().read();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
super.onDestroyView();
|
|
|
|
unbinder.unbind();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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:
|
|
|
|
new LogManager().read();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_save:
|
|
|
|
new LogManager().save();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_clear:
|
|
|
|
new LogManager().clear();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
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) {
|
|
|
|
new Handler().postDelayed(() -> onOptionsItemSelected(mClickedMenuItem), 500);
|
|
|
|
}
|
|
|
|
} else {
|
2017-02-14 21:24:02 +00:00
|
|
|
SnackbarMaker.make(txtLog, R.string.permissionNotGranted, Snackbar.LENGTH_LONG).show();
|
2017-01-26 19:38:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 11:49:46 +00:00
|
|
|
public class LogManager extends SerialTask<Object, Void, Object> {
|
2017-01-26 19:38:53 +00:00
|
|
|
|
|
|
|
int mode;
|
|
|
|
File targetFile;
|
|
|
|
|
|
|
|
@SuppressLint("DefaultLocale")
|
|
|
|
@Override
|
|
|
|
protected Object doInBackground(Object... params) {
|
|
|
|
mode = (int) params[0];
|
|
|
|
switch (mode) {
|
|
|
|
case 0:
|
|
|
|
List<String> logList = Utils.readFile(MAGISK_LOG);
|
|
|
|
|
|
|
|
if (Utils.isValidShellResponse(logList)) {
|
|
|
|
StringBuilder llog = new StringBuilder(15 * 10 * 1024);
|
|
|
|
for (String s : logList) {
|
|
|
|
llog.append(s).append("\n");
|
|
|
|
}
|
|
|
|
return llog.toString();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
Shell.su("echo > " + MAGISK_LOG);
|
2017-02-14 21:24:02 +00:00
|
|
|
SnackbarMaker.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
|
2017-01-26 19:38:53 +00:00
|
|
|
return "";
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-20 19:30:37 +00:00
|
|
|
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
2017-01-26 19:38:53 +00:00
|
|
|
return false;
|
2017-02-20 19:30:37 +00:00
|
|
|
}
|
2017-01-26 19:38:53 +00:00
|
|
|
|
|
|
|
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())
|
2017-02-20 19:30:37 +00:00
|
|
|
|| (targetFile.exists() && !targetFile.delete())) {
|
2017-01-26 19:38:53 +00:00
|
|
|
return false;
|
2017-02-20 19:30:37 +00:00
|
|
|
}
|
2017-01-26 19:38:53 +00:00
|
|
|
|
|
|
|
List<String> in = Utils.readFile(MAGISK_LOG);
|
|
|
|
|
|
|
|
if (Utils.isValidShellResponse(in)) {
|
|
|
|
try (FileWriter out = new FileWriter(targetFile)) {
|
|
|
|
for (String line : in)
|
|
|
|
out.write(line + "\n");
|
|
|
|
return true;
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Object o) {
|
|
|
|
if (o == null) return;
|
|
|
|
boolean bool;
|
|
|
|
String llog;
|
|
|
|
switch (mode) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
llog = (String) o;
|
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
if (TextUtils.isEmpty(llog))
|
|
|
|
txtLog.setText(R.string.log_is_empty);
|
|
|
|
else
|
|
|
|
txtLog.setText(llog);
|
|
|
|
svLog.post(() -> svLog.scrollTo(0, txtLog.getHeight()));
|
|
|
|
hsvLog.post(() -> hsvLog.scrollTo(0, 0));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
bool = (boolean) o;
|
2017-02-20 19:30:37 +00:00
|
|
|
if (bool) {
|
2017-01-26 19:38:53 +00:00
|
|
|
Toast.makeText(getActivity(), targetFile.toString(), Toast.LENGTH_LONG).show();
|
2017-02-20 19:30:37 +00:00
|
|
|
} else {
|
2017-01-26 19:38:53 +00:00
|
|
|
Toast.makeText(getActivity(), getString(R.string.logs_save_failed), Toast.LENGTH_LONG).show();
|
2017-02-20 19:30:37 +00:00
|
|
|
}
|
2017-01-26 19:38:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void read() {
|
|
|
|
exec(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clear() {
|
|
|
|
exec(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void save() {
|
|
|
|
exec(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|