1+ package com .smartpack .kernelprofiler .utils ;
2+
3+ import android .Manifest ;
4+ import android .os .Bundle ;
5+ import android .os .Environment ;
6+
7+ import androidx .annotation .Nullable ;
8+ import androidx .appcompat .app .AlertDialog ;
9+ import androidx .appcompat .app .AppCompatActivity ;
10+ import androidx .appcompat .widget .AppCompatEditText ;
11+ import androidx .appcompat .widget .AppCompatImageButton ;
12+ import androidx .appcompat .widget .AppCompatTextView ;
13+ import androidx .core .app .ActivityCompat ;
14+
15+ import com .smartpack .kernelprofiler .R ;
16+ import com .smartpack .kernelprofiler .utils .root .RootUtils ;
17+
18+ import org .json .JSONException ;
19+ import org .json .JSONObject ;
20+
21+ /**
22+ * Created by sunilpaulmathew <sunil.kde@gmail.com> on May 28, 2020
23+ */
24+
25+ public class EditConfigActivity extends AppCompatActivity {
26+
27+ AppCompatEditText mConfigTitleHint ;
28+ AppCompatEditText mDescriptionHint ;
29+ AppCompatEditText mDefaultHint ;
30+ AppCompatEditText mDeveloperHint ;
31+ AppCompatEditText mSupportHint ;
32+ AppCompatEditText mDonationsHint ;
33+ AppCompatTextView mTitle ;
34+
35+ @ Override
36+ protected void onCreate (@ Nullable Bundle savedInstanceState ) {
37+ super .onCreate (savedInstanceState );
38+ setContentView (R .layout .activity_createconfig );
39+
40+ AppCompatImageButton mBack = findViewById (R .id .back_button );
41+ mBack .setOnClickListener (v -> onBackPressed ());
42+ AppCompatImageButton mSave = findViewById (R .id .save_button );
43+ AppCompatImageButton mCheck = findViewById (R .id .check_button );
44+ mConfigTitleHint = findViewById (R .id .config_title_hint );
45+ if (KP .getCustomTitle () != null ) {
46+ mConfigTitleHint .setText (KP .getCustomTitle ());
47+ }
48+ mDescriptionHint = findViewById (R .id .config_description_hint );
49+ if (KP .getCustomDescription () != null ) {
50+ mDescriptionHint .setText (KP .getCustomDescription ());
51+ }
52+ mDefaultHint = findViewById (R .id .default_profile_hint );
53+ if (KP .getDefaultProfile () != null ) {
54+ mDefaultHint .setText (KP .getDefaultProfile ());
55+ }
56+ mDeveloperHint = findViewById (R .id .developer_hint );
57+ if (KP .getDeveloper () != null ) {
58+ mDeveloperHint .setText (KP .getDeveloper ());
59+ }
60+ mSupportHint = findViewById (R .id .support_hint );
61+ if (KP .getSupport () != null ) {
62+ mSupportHint .setText (KP .getSupport ());
63+ }
64+ mDonationsHint = findViewById (R .id .donations_hint );
65+ if (KP .getDonation () != null ) {
66+ mDonationsHint .setText (KP .getDonation ());
67+ }
68+ mTitle = findViewById (R .id .title );
69+ mTitle .setText (getString (R .string .edit_config ));
70+ mSave .setOnClickListener (v -> {
71+ if (Utils .checkWriteStoragePermission (this )) {
72+ if (mConfigTitleHint .getText () != null && !mConfigTitleHint .getText ().toString ().equals ("" )) {
73+ new AlertDialog .Builder (this )
74+ .setMessage (getString (R .string .edit_config_message ))
75+ .setNegativeButton (getString (R .string .cancel ), (dialog1 , id1 ) -> {
76+ })
77+ .setPositiveButton (getString (R .string .ok ), (dialog1 , id1 ) -> {
78+ createConfig ();
79+ })
80+ .show ();
81+ } else {
82+ Utils .snackbar (mTitle , getString (R .string .title_empty_message ));
83+ }
84+ } else {
85+ ActivityCompat .requestPermissions (this , new String []{
86+ Manifest .permission .WRITE_EXTERNAL_STORAGE },1 );
87+ Utils .snackbar (mTitle , getString (R .string .storage_access_denied ) + " " +
88+ Environment .getExternalStorageDirectory ().toString ());
89+ }
90+ });
91+ mCheck .setOnClickListener (v -> {
92+ if (mConfigTitleHint .getText () != null && !mConfigTitleHint .getText ().toString ().equals ("" ) &&
93+ RootUtils .runAndGetOutput ("uname -a" ).contains (mConfigTitleHint .getText ())) {
94+ Utils .snackbar (mTitle , getString (R .string .success ));
95+ } else {
96+ Utils .snackbar (mTitle , getString (R .string .failed ));
97+ }
98+ });
99+ }
100+
101+ private void createConfig () {
102+ try {
103+ JSONObject obj = new JSONObject ();
104+ obj .put ("title" , mConfigTitleHint .getText ());
105+ obj .put ("description" , mDescriptionHint .getText ());
106+ obj .put ("default" , mDefaultHint .getText ());
107+ obj .put ("developer" , mDeveloperHint .getText ());
108+ obj .put ("support" , mSupportHint .getText ());
109+ obj .put ("donations" , mDonationsHint .getText ());
110+ Utils .create (obj .toString (), "/data/kernel_profiler/kernelprofiler.json" );
111+ Utils .snackbarIndenite (mTitle , getString (R .string .edit_config_saved ));
112+ } catch (JSONException ignored ) {
113+ }
114+ }
115+
116+ @ Override
117+ public void onBackPressed () {
118+ super .onBackPressed ();
119+ }
120+
121+ }
0 commit comments