11package com .surcumference .fingerprint .view ;
22
33import android .content .Context ;
4+ import android .content .Intent ;
45import android .graphics .Color ;
56import android .graphics .drawable .ColorDrawable ;
67import android .util .AttributeSet ;
1314import androidx .annotation .NonNull ;
1415import androidx .annotation .Nullable ;
1516
17+ import com .hjq .toast .Toaster ;
1618import com .surcumference .fingerprint .Lang ;
1719import com .surcumference .fingerprint .R ;
1820import com .surcumference .fingerprint .adapter .PreferenceAdapter ;
1921import com .surcumference .fingerprint .util .Config ;
22+ import com .surcumference .fingerprint .util .DateUtils ;
2023import com .surcumference .fingerprint .util .DpUtils ;
24+ import com .surcumference .fingerprint .util .FileUtils ;
25+ import com .surcumference .fingerprint .util .LogcatManager ;
26+ import com .surcumference .fingerprint .util .Task ;
27+ import com .surcumference .fingerprint .util .log .L ;
2128
29+ import java .io .File ;
2230import java .util .ArrayList ;
31+ import java .util .Date ;
2332import java .util .List ;
33+ import java .util .Locale ;
2434
2535
2636/**
@@ -33,6 +43,8 @@ public class AdvanceSettingsView extends DialogFrameLayout implements AdapterVie
3343 private PreferenceAdapter mListAdapter ;
3444 private ListView mListView ;
3545
46+ private static LogcatManager sLogcatManager ;
47+
3648 public AdvanceSettingsView (@ NonNull Context context ) {
3749 super (context );
3850 init (context );
@@ -49,6 +61,17 @@ public AdvanceSettingsView(@NonNull Context context, @Nullable AttributeSet attr
4961 }
5062
5163 private void init (Context context ) {
64+ if (sLogcatManager == null ) {
65+ File logFile = FileUtils .getSharableFile (context , "flog/" + context .getPackageName () + ".log" );
66+ try {
67+ FileUtils .delete (logFile .getParentFile ());
68+ logFile .getParentFile ().mkdirs ();
69+ } catch (Exception e ) {
70+ L .e (e );
71+ }
72+ sLogcatManager = new LogcatManager (logFile );
73+ sLogcatManager .getTargetFile ().deleteOnExit ();
74+ }
5275 LinearLayout rootVerticalLayout = new LinearLayout (context );
5376 rootVerticalLayout .setOrientation (LinearLayout .VERTICAL );
5477
@@ -65,6 +88,11 @@ private void init(Context context) {
6588 mListView .setDivider (new ColorDrawable (Color .TRANSPARENT ));
6689 mSettingsDataList .add (new PreferenceAdapter .Data (Lang .getString (R .id .settings_title_no_fingerprint_icon ), Lang .getString (R .id .settings_sub_title_no_fingerprint_icon ), true , Config .from (context ).isShowFingerprintIcon ()));
6790 mSettingsDataList .add (new PreferenceAdapter .Data (Lang .getString (R .id .settings_title_use_biometric_api ), Lang .getString (R .id .settings_sub_title_use_biometric_api ), true , Config .from (context ).isUseBiometricApi ()));
91+ if (sLogcatManager .isRunning ()) {
92+ mSettingsDataList .add (new PreferenceAdapter .Data (Lang .getString (R .id .settings_title_stop_logcat ), Lang .getString (R .id .settings_sub_title_stop_logcat )));
93+ } else {
94+ mSettingsDataList .add (new PreferenceAdapter .Data (Lang .getString (R .id .settings_title_start_logcat ), Lang .getString (R .id .settings_sub_title_start_logcat )));
95+ }
6896 mListAdapter = new PreferenceAdapter (mSettingsDataList );
6997
7098 rootVerticalLayout .addView (lineView , new LinearLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , DpUtils .dip2px (context , 2 )));
@@ -102,7 +130,45 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
102130 data .selectionState = !data .selectionState ;
103131 config .setUseBiometricApi (data .selectionState );
104132 mListAdapter .notifyDataSetChanged ();
133+ } else if (Lang .getString (R .id .settings_title_start_logcat ).equals (data .title )) {
134+ sLogcatManager .startLogging (5 * 60 * 1000 /** 5min */ );
135+ data .title = Lang .getString (R .id .settings_title_stop_logcat );
136+ data .subTitle = Lang .getString (R .id .settings_sub_title_stop_logcat );
137+ mListAdapter .notifyDataSetChanged ();
138+ File logFile = sLogcatManager .getTargetFile ();
139+ Toaster .showLong (String .format (Locale .getDefault (),
140+ Lang .getString (R .id .toast_start_logging ), logFile .getAbsoluteFile ()));
141+ } else if (Lang .getString (R .id .settings_title_stop_logcat ).equals (data .title )) {
142+ sLogcatManager .stopLogging ();
143+ data .title = Lang .getString (R .id .settings_title_start_logcat );
144+ data .subTitle = Lang .getString (R .id .settings_sub_title_start_logcat );
145+ mListAdapter .notifyDataSetChanged ();
146+ File logFile = sLogcatManager .getTargetFile ();
147+ try {
148+ File logShareFile = new File (logFile .getParentFile (), context .getPackageName () + "-" + DateUtils .toString (new Date ()).replaceAll ("[: ]" , "-" ) + ".log" );
149+ if (logFile .renameTo (logShareFile )) {
150+ logFile = logShareFile ;
151+ }
152+ } catch (Exception e ) {
153+ L .e (e );
154+ }
155+ logFile .deleteOnExit ();
156+ File finalLogFile = logFile ;
157+ Task .onMain (500 , () -> Toaster .showLong (String .format (Locale .getDefault (),
158+ Lang .getString (R .id .toast_stop_logging ), finalLogFile .getAbsoluteFile ())));
159+ shareFile (logFile );
105160 }
106161 }
107162
163+ private void shareFile (File targetFile ) {
164+ try {
165+ Context context = getContext ();
166+ Intent intent = new Intent (Intent .ACTION_SEND );
167+ intent .setType ("*/*" );
168+ intent .putExtra (Intent .EXTRA_STREAM , FileUtils .getUri (context , targetFile ));
169+ context .startActivity (Intent .createChooser (intent , "Share File" ));
170+ } catch (Exception e ) {
171+ L .e (e );
172+ }
173+ }
108174}
0 commit comments