Log fragment completed

This commit is contained in:
dvdandroid 2016-08-21 17:21:37 +02:00 committed by topjohnwu
parent 5279226f36
commit e6b951c62a
17 changed files with 432 additions and 35 deletions

View File

@ -1,13 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.topjohnwu.magisk"
xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@ -15,7 +19,9 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".WelcomeActivity">
<activity
android:name=".WelcomeActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

View File

@ -2,6 +2,7 @@ package com.topjohnwu.magisk;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
@ -30,6 +31,9 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
@BindView(R.id.drawer_layout) DrawerLayout drawer;
@BindView(R.id.nav_view) NavigationView navigationView;
@IdRes
private int mSelectedId = R.id.modules;// for now
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -51,21 +55,31 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
}
};
drawer.setDrawerListener(toggle);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
//noinspection ResourceType
mSelectedId = savedInstanceState == null ? mSelectedId : savedInstanceState.getInt(SELECTED_ITEM_ID);
navigationView.setCheckedItem(mSelectedId);
if (savedInstanceState != null) {
if (savedInstanceState == null) {
mDrawerHandler.removeCallbacksAndMessages(null);
mDrawerHandler.postDelayed(new Runnable() {
@Override
public void run() {
navigate(savedInstanceState.getInt(SELECTED_ITEM_ID));
navigate(mSelectedId);
}
}, 250);
}
navigationView.setNavigationItemSelectedListener(this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SELECTED_ITEM_ID, mSelectedId);
}
@Override
@ -79,7 +93,7 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
@Override
public boolean onNavigationItemSelected(@NonNull final MenuItem menuItem) {
menuItem.setChecked(true);
mSelectedId = menuItem.getItemId();
mDrawerHandler.removeCallbacksAndMessages(null);
mDrawerHandler.postDelayed(new Runnable() {
@Override

View File

@ -27,7 +27,7 @@ public class ModulesAdapter extends RecyclerView.Adapter<ModulesAdapter.ViewHold
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_module, parent, false);
return new ViewHolder(view);
}

View File

@ -1,5 +1,275 @@
package com.topjohnwu.magisk.ui;
public class LogFragment extends android.support.v4.app.Fragment {
// private static final String MAGISK_LOG = "/cache/magisk.log";
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;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.ui.utils.Utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import butterknife.BindView;
import butterknife.ButterKnife;
public class LogFragment extends Fragment {
private static final File MAGISK_LOG = new File("/cache/magisk.log");
@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() {
new LogsReader().execute(MAGISK_LOG);
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() {
try {
new FileOutputStream(MAGISK_LOG).close();
MAGISK_LOG.delete();
txtLog.setText(R.string.log_is_empty);
Snackbar.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
reloadErrorLog();
} catch (IOException e) {
Toast.makeText(getActivity(), getResources().getString(R.string.logs_clear_failed) + "\n" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
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);
try {
FileInputStream in = new FileInputStream(MAGISK_LOG);
FileOutputStream out = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
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;
}
}
private class LogsReader extends AsyncTask<File, Integer, String> {
private static final int MAX_LOG_SIZE = 1000 * 1024; // 1000 KB
private long skipLargeFile(BufferedReader is, long length) throws IOException {
if (length < MAX_LOG_SIZE)
return 0;
long skipped = length - MAX_LOG_SIZE;
long yetToSkip = skipped;
do {
yetToSkip -= is.skip(yetToSkip);
} while (yetToSkip > 0);
int c;
do {
c = is.read();
if (c == -1)
break;
skipped++;
} while (c != '\n');
return skipped;
}
@Override
protected void onPreExecute() {
Utils.executeCommand("chmod 777 /cache");
Utils.executeCommand("chmod 777 /cache/magisk.log");
txtLog.setText("");
}
@Override
protected String doInBackground(File... log) {
Thread.currentThread().setPriority(Thread.NORM_PRIORITY + 2);
StringBuilder llog = new StringBuilder(15 * 10 * 1024);
try {
File logfile = log[0];
BufferedReader br;
br = new BufferedReader(new FileReader(logfile));
long skipped = skipLargeFile(br, logfile.length());
if (skipped > 0) {
llog.append("-----------------\n");
llog.append("Log too long");
llog.append("\n-----------------\n\n");
}
char[] temp = new char[1024];
int read;
while ((read = br.read(temp)) > 0) {
llog.append(temp, 0, read);
}
br.close();
} catch (IOException e) {
llog.append("Cannot read log:\n");
llog.append(e.getMessage());
}
return llog.toString();
}
@Override
protected void onPostExecute(String llog) {
progressBar.setVisibility(View.GONE);
txtLog.setText(llog);
if (llog.length() == 0)
txtLog.setText(R.string.log_is_empty);
}
}
}

View File

@ -25,7 +25,7 @@ import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ModulesFragment extends android.support.v4.app.Fragment {
public class ModulesFragment extends Fragment {
private static final String MAGISK_PATH = "/magisk";
private static final String MAGISK_CACHE_PATH = "/cache/magisk";

View File

@ -1,4 +1,6 @@
package com.topjohnwu.magisk.ui;
public class RootFragment extends android.support.v4.app.Fragment {
import android.support.v4.app.Fragment;
public class RootFragment extends Fragment {
}

View File

@ -1,8 +1,8 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#757575"
android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#000"
android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M17,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,7l-4,-4zM12,19c-1.66,0 -3,-1.34 -3,-3s1.34,-3 3,-3 3,1.34 3,3 -1.34,3 -3,3zM15,9L5,9L5,5h10v4z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z"/>
</vector>

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.ModulesActivity">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
@ -65,7 +66,6 @@
</LinearLayout>
<!-- FIXME-->
<ImageView
android:id="@+id/overflow"
android:layout_width="wrap_content"
@ -73,7 +73,8 @@
android:focusable="false"
android:gravity="center"
android:padding="3dp"
android:src="@drawable/ic_menu_overflow_material"/>
android:src="@drawable/ic_menu_overflow_material"
tools:ignore="ContentDescription"/>
</LinearLayout>

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
<ScrollView
android:id="@+id/svLog"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:id="@+id/hsvLog"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ScrollViewSize">
<TextView
android:id="@+id/txtLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textIsSelectable="true"/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_save"
android:icon="@drawable/ic_save"
android:title="@string/menuSaveToSd"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/menu_send"
android:icon="@drawable/ic_send"
android:title="@string/menuSend"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/menu_clear"
android:icon="@drawable/ic_delete_black"
android:title="@string/menuClearLog"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/menu_refresh"
android:icon="@drawable/ic_refresh"
android:title="@string/menuReload"
app:showAsAction="ifRoom"/>
</menu>

View File

@ -18,13 +18,11 @@
<string name="root_none_info">Safety Net (Android Pay) should work</string>
<string name="magisk_version">Magisk v%1$s</string>
<string name="selinux_samsung">Samsung do not support switching SELinux status!</string>
<string name="loading">Loading...</string>
<string name="title_activity_welcome">WelcomeActivity</string>
<string name="loading">Loading…</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="action_settings">Settings</string>
<string name="root">Root</string>
<string name="modules">Modules</string>
<string name="log">Log</string>
@ -32,4 +30,15 @@
<string name="disable">Disable</string>
<string name="remove_file_created">Module will be removed at next reboot</string>
<string name="disable_file_created">Module will be disabled at next reboot</string>
<string name="menuSaveToSd">Save to SD</string>
<string name="menuSend">Send</string>
<string name="menuReload">Reload</string>
<string name="sdcard_not_writable">SD card not found or not writable</string>
<string name="menuClearLog">Clear log now</string>
<string name="logs_cleared">Log successfully cleared</string>
<string name="logs_clear_failed">Could not clear the log:</string>
<string name="log_is_empty">Log is empty</string>
<string name="logs_save_failed">Could not write log to SD card:</string>
<string name="permissionNotGranted">This feature will not work without permission to write external storage.</string>
</resources>

View File

@ -17,7 +17,7 @@
<item name="colorPrimary">@color/primary</item>
<item name="android:elevation">4dp</item>
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColorSecondary">@color/secondary_text</item>
<item name="android:textColorSecondary">@color/primary_text</item> <!-- force -->
<item name="actionMenuTextColor">@color/icons</item>
</style>