Browse Source

add simple logcat viewer

Andrew Dolgov 10 months ago
parent
commit
d819f6c4d9

+ 4 - 0
org.fox.ttrss/src/main/AndroidManifest.xml

@@ -63,6 +63,10 @@
             android:name=".CommonActivity"
             android:label="@string/app_name" >
         </activity>
+        <activity
+            android:name=".LogcatActivity"
+            android:label="@string/logcat_title" >
+        </activity>
         <activity
             android:name=".tasker.TaskerSettingsActivity"
             android:label="@string/app_name" >

+ 116 - 0
org.fox.ttrss/src/main/java/org/fox/ttrss/LogcatActivity.java

@@ -0,0 +1,116 @@
+package org.fox.ttrss;
+
+import android.os.Bundle;
+import android.support.v4.widget.SwipeRefreshLayout;
+import android.support.v7.widget.Toolbar;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+
+import icepick.State;
+
+public class LogcatActivity extends CommonActivity {
+    private static final int MAX_LOG_ENTRIES = 500;
+    private final String TAG = this.getClass().getSimpleName();
+    @State protected ArrayList<String> m_items = new ArrayList<>();
+    ArrayAdapter<String> m_adapter;
+    ListView m_list;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+
+        setTheme(R.style.DarkTheme);
+
+        super.onCreate(savedInstanceState);
+
+        setContentView(R.layout.activity_logcat);
+
+        Toolbar toolbar = findViewById(R.id.toolbar);
+        setSupportActionBar(toolbar);
+
+        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+        getSupportActionBar().setHomeButtonEnabled(true);
+
+        if (savedInstanceState == null) {
+            refresh();
+        }
+
+        m_adapter = new ArrayAdapter<>(this, R.layout.logcat_row, m_items);
+
+        m_list = findViewById(R.id.logcat_output);
+        m_list.setAdapter(m_adapter);
+
+        final SwipeRefreshLayout swipeLayout = findViewById(R.id.logcat_swipe_container);
+
+        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
+            @Override
+            public void onRefresh() {
+                refresh();
+                swipeLayout.setRefreshing(false);
+            }
+        });
+    }
+
+    private void refresh() {
+        m_items.clear();
+
+        try {
+            Process process = Runtime.getRuntime().exec("logcat -d -t " +  MAX_LOG_ENTRIES);
+            BufferedReader bufferedReader = new BufferedReader(
+                    new InputStreamReader(process.getInputStream()));
+
+            String line;
+
+            while ((line = bufferedReader.readLine()) != null) {
+                m_items.add(0, line);
+            }
+
+        } catch (Exception e) {
+            m_items.add(e.toString());
+        }
+
+        if (m_adapter != null) m_adapter.notifyDataSetChanged();
+    }
+
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        MenuInflater inflater = getMenuInflater();
+        inflater.inflate(R.menu.activity_logcat, menu);
+
+        return true;
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        int id = item.getItemId();
+
+        switch (id) {
+            case android.R.id.home:
+                onBackPressed();
+                return true;
+            case R.id.logcat_copy:
+                shareLogcat();
+                return true;
+            case R.id.logcat_refresh:
+                refresh();
+                return true;
+            default:
+                return super.onOptionsItemSelected(item);
+        }
+    }
+
+    private void shareLogcat() {
+        StringBuilder buf = new StringBuilder();
+
+        for (String item : m_items)
+            buf.append(item + "\n");
+
+        copyToClipboard(buf.toString());
+    }
+}

+ 10 - 0
org.fox.ttrss/src/main/java/org/fox/ttrss/PreferencesFragment.java

@@ -1,6 +1,7 @@
 package org.fox.ttrss;
 
 import android.app.Activity;
+import android.content.Intent;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.os.Bundle;
@@ -19,6 +20,15 @@ public class PreferencesFragment extends PreferenceFragment {
         // Load the preferences from an XML resource
         addPreferencesFromResource(R.xml.preferences);
 
+        findPreference("show_logcat").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+            @Override
+            public boolean onPreferenceClick(Preference preference) {
+                Intent intent = new Intent(getActivity(), LogcatActivity.class);
+                startActivity(intent);
+                return false;
+            }
+        });
+
         findPreference("network_settings").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
             @Override
             public boolean onPreferenceClick(Preference preference) {

+ 26 - 0
org.fox.ttrss/src/main/res/layout/activity_logcat.xml

@@ -0,0 +1,26 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:background="?android:colorBackground"
+    android:fitsSystemWindows="true"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent" >
+
+    <include layout="@layout/toolbar" android:id="@+id/toolbar" />
+
+    <android.support.v4.widget.SwipeRefreshLayout
+        android:id="@+id/logcat_swipe_container"
+        android:layout_below="@+id/toolbar"
+        android:layout_alignParentBottom="true"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentStart="true"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+
+        <ListView
+            android:id="@+id/logcat_output"
+            android:scrollbars="vertical"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"/>
+
+    </android.support.v4.widget.SwipeRefreshLayout>
+
+</RelativeLayout>

+ 8 - 0
org.fox.ttrss/src/main/res/layout/logcat_row.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@android:id/text1"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:padding="4sp"
+    android:textSize="12sp"
+    android:gravity="center_vertical"/>

+ 15 - 0
org.fox.ttrss/src/main/res/menu/activity_logcat.xml

@@ -0,0 +1,15 @@
+<?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/logcat_refresh"
+        android:icon="@drawable/ic_refresh"
+        app:showAsAction="ifRoom"
+        android:title="@string/logcat_refresh"/>
+
+    <item
+        android:id="@+id/logcat_copy"
+        android:icon="@drawable/ic_content_copy"
+        app:showAsAction="ifRoom"
+        android:title="@string/logcat_copy"/>
+
+</menu>

+ 0 - 0
org.fox.ttrss/src/main/res/menu/content_gallery_entry.xml


+ 3 - 0
org.fox.ttrss/src/main/res/values/strings.xml

@@ -260,4 +260,7 @@
     <string name="offline_tap_to_switch">Tap to switch offline</string>
     <string name="offline_no_articles">No articles downloaded</string>
     <string name="prefs_network_settings">More settings...</string>
+    <string name="logcat_refresh">Refresh</string>
+    <string name="logcat_copy">Copy to clipboard</string>
+    <string name="logcat_title">Log viewer</string>
 </resources>

+ 4 - 0
org.fox.ttrss/src/main/res/xml/preferences.xml

@@ -193,6 +193,10 @@
     </PreferenceCategory>
     <PreferenceCategory android:title="@string/debugging" >
 
+        <Preference
+            android:key="show_logcat"
+            android:title="@string/logcat_title" />
+
         <org.fox.ttrss.util.LessBrokenSwitchPreference
             android:defaultValue="true"
             android:key="webview_hardware_accel"