2016-08-22 21:18:28 +00:00
|
|
|
package com.topjohnwu.magisk;
|
2016-08-21 13:29:42 +00:00
|
|
|
|
2016-08-21 15:21:37 +00:00
|
|
|
import android.Manifest;
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
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.support.v4.app.Fragment;
|
|
|
|
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-08-25 10:08:07 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Shell;
|
2016-08-24 21:58:15 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
2016-08-21 15:21:37 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
2016-08-24 21:58:15 +00:00
|
|
|
import java.io.FileWriter;
|
2016-08-21 15:21:37 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Calendar;
|
2016-08-24 21:58:15 +00:00
|
|
|
import java.util.List;
|
2016-08-22 19:50:46 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2016-08-21 15:21:37 +00:00
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public class LogFragment extends Fragment {
|
|
|
|
|
2016-08-24 21:58:15 +00:00
|
|
|
private static final String MAGISK_LOG = "/cache/magisk.log";
|
2016-08-21 15:21:37 +00: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 void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
|
|
|
reloadErrorLog();
|
|
|
|
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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:
|
|
|
|
reloadErrorLog();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_send:
|
|
|
|
try {
|
|
|
|
send();
|
|
|
|
} catch (NullPointerException ignored) {
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case R.id.menu_save:
|
|
|
|
save();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_clear:
|
|
|
|
clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void reloadErrorLog() {
|
2016-08-24 21:58:15 +00:00
|
|
|
new LogsManager(true).execute();
|
2016-08-21 15:21:37 +00:00
|
|
|
svLog.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
svLog.scrollTo(0, txtLog.getHeight());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
hsvLog.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
hsvLog.scrollTo(0, 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clear() {
|
2016-08-24 21:58:15 +00:00
|
|
|
new LogsManager(false).execute();
|
|
|
|
reloadErrorLog();
|
2016-08-21 15:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void send() {
|
|
|
|
Intent sendIntent = new Intent();
|
|
|
|
sendIntent.setAction(Intent.ACTION_SEND);
|
|
|
|
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(save()));
|
|
|
|
sendIntent.setType("application/html");
|
|
|
|
startActivity(Intent.createChooser(sendIntent, getResources().getString(R.string.menuSend)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@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(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
onOptionsItemSelected(mClickedMenuItem);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Snackbar.make(txtLog, R.string.permissionNotGranted, Snackbar.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("DefaultLocale")
|
|
|
|
private File save() {
|
|
|
|
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
|
|
|
Snackbar.make(txtLog, R.string.sdcard_not_writable, Snackbar.LENGTH_LONG).show();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Magisk/");
|
|
|
|
dir.mkdir();
|
|
|
|
|
|
|
|
File targetFile = new File(dir, filename);
|
2016-08-24 21:58:15 +00:00
|
|
|
List<String> in = Utils.readFile(MAGISK_LOG);
|
2016-08-21 15:21:37 +00:00
|
|
|
|
|
|
|
try {
|
2016-08-24 21:58:15 +00:00
|
|
|
FileWriter out = new FileWriter(targetFile);
|
|
|
|
for (String line : in) {
|
|
|
|
out.write(line + "\n");
|
2016-08-21 15:21:37 +00:00
|
|
|
}
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
Toast.makeText(getActivity(), targetFile.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
return targetFile;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Toast.makeText(getActivity(), getResources().getString(R.string.logs_save_failed) + "\n" + e.getMessage(), Toast.LENGTH_LONG).show();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:58:15 +00:00
|
|
|
private class LogsManager extends AsyncTask<Void, Integer, String> {
|
2016-08-21 15:21:37 +00:00
|
|
|
|
2016-08-24 21:58:15 +00:00
|
|
|
private boolean readLog;
|
2016-08-21 15:21:37 +00:00
|
|
|
|
2016-08-24 21:58:15 +00:00
|
|
|
public LogsManager(boolean read) {
|
|
|
|
readLog = read;
|
2016-08-21 15:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
txtLog.setText("");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-08-24 21:58:15 +00:00
|
|
|
protected String doInBackground(Void... voids) {
|
|
|
|
if (readLog) {
|
|
|
|
List<String> logList = Utils.readFile(MAGISK_LOG);
|
2016-08-21 15:21:37 +00:00
|
|
|
|
2016-08-24 21:58:15 +00:00
|
|
|
StringBuilder llog = new StringBuilder(15 * 10 * 1024);
|
|
|
|
for (String s : logList) {
|
|
|
|
llog.append(s).append("\n");
|
2016-08-21 15:21:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:58:15 +00:00
|
|
|
return llog.toString();
|
|
|
|
} else {
|
|
|
|
if (Utils.removeFile(MAGISK_LOG)) {
|
|
|
|
Snackbar.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
|
|
|
|
} else {
|
|
|
|
Snackbar.make(txtLog, R.string.logs_clear_failed, Snackbar.LENGTH_SHORT).show();
|
2016-08-21 15:21:37 +00:00
|
|
|
}
|
2016-08-24 21:58:15 +00:00
|
|
|
return "";
|
2016-08-21 15:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(String llog) {
|
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
txtLog.setText(llog);
|
|
|
|
|
|
|
|
if (llog.length() == 0)
|
|
|
|
txtLog.setText(R.string.log_is_empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
}
|