55import android .app .Activity ;
66import android .content .Context ;
77import android .content .SharedPreferences ;
8+ import android .os .Build ;
89import android .os .StrictMode ;
910import android .view .View ;
1011import android .view .inputmethod .InputMethodManager ;
1314import android .widget .Toast ;
1415
1516// Material imports
17+ import androidx .security .crypto .EncryptedSharedPreferences ;
18+ import androidx .security .crypto .MasterKeys ;
19+
1620import com .google .android .material .snackbar .BaseTransientBottomBar ;
1721import com .google .android .material .snackbar .Snackbar ;
1822
@@ -42,17 +46,47 @@ public class Functions {
4246 // (this does mean no responses can be grabbed by this function)
4347 // accepts string of command, context for sharedPrefs, view for snackbar
4448 // returns void
45- static public int executeSSHcommand (String command , Context context , View baseView ) {
46- SharedPreferences sharedPreferences = context .getSharedPreferences ("credentials" , Context .MODE_PRIVATE ); // open credentials file
47- String user = sharedPreferences .getString ("username" , "pi" ); // use credentials unless not set, then use username pi
48- String password = sharedPreferences .getString ("password" , "raspberry" ); // use credentials unless not set, then use password raspberry
49- String host = "192.168.4.1" ; // use IP address, will end up adding a field for this later
50- int port = sharedPreferences .getInt ("port" , 22 ); // use credentials unless not set, then use port 22
49+ static public void executeSSHcommand (String command , Context context , View baseView ) {
50+ int sdk = Build .VERSION .SDK_INT ;
51+ String user ;
52+ String password ;
53+ String host ;
54+ int port ;
55+ SharedPreferences sharedPreferences ;
56+ if (sdk >= 23 ){
57+ String masterKeyAlias ;
58+ try {
59+ masterKeyAlias = MasterKeys .getOrCreate (MasterKeys .AES256_GCM_SPEC ); // get master key
60+ sharedPreferences = EncryptedSharedPreferences .create (
61+ "secret_shared_prefs" ,
62+ masterKeyAlias ,
63+ context ,
64+ EncryptedSharedPreferences .PrefKeyEncryptionScheme .AES256_SIV ,
65+ EncryptedSharedPreferences .PrefValueEncryptionScheme .AES256_GCM
66+ );
67+ } catch (Exception e ) {
68+ e .printStackTrace ();
69+ Snackbar .make (baseView , "Failed to create encryption keys (IOException or SecurityException)" , BaseTransientBottomBar .LENGTH_LONG )
70+ .show ();
71+ return ;
72+ }
73+ user = sharedPreferences .getString (context .getString (R .string .username_sharedprop ), "pi" );
74+ password = sharedPreferences .getString (context .getString (R .string .password_sharedprop ), "raspberry" );
75+ host = sharedPreferences .getString (context .getString (R .string .host_sharedprop ), "192.168.4.1" );
76+ port = sharedPreferences .getInt (context .getString (R .string .port_sharedprop ), 22 );
77+ } else {
78+ sharedPreferences = context .getSharedPreferences ("credentials" , Context .MODE_PRIVATE ); // open credentials file
79+ user = sharedPreferences .getString ("username" , "pi" ); // use credentials unless not set, then use username pi
80+ password = sharedPreferences .getString ("password" , "raspberry" ); // use credentials unless not set, then use password raspberry
81+ host = sharedPreferences .getString ("host" , "192.168.4.1" ); // use IP address, will end up adding a field for this later
82+ port = sharedPreferences .getInt ("port" , 22 ); // use credentials unless not set, then use port 22
83+ }
5184 StrictMode .ThreadPolicy policy = new StrictMode .ThreadPolicy .Builder ()
5285 .permitAll ().build ();
5386 StrictMode .setThreadPolicy (policy ); // just a workaround
5487 try {
5588 JSch jsch = new JSch (); // instantiate a new JSch call
89+ assert host != null ;
5690 Session session = jsch .getSession (user , host , port ); // set session credentials
5791 session .setPassword (password ); // set password
5892 java .util .Properties config = new java .util .Properties ();
@@ -70,15 +104,13 @@ static public int executeSSHcommand(String command, Context context, View baseVi
70104 Snackbar .make (baseView , R .string .connect_failed_snack , BaseTransientBottomBar .LENGTH_LONG )
71105 .show (); // tell the user the command failed
72106 }
73- return 0 ;
74107 }
75108
76109 // readFile function
77110 // Reads in button data and places buttons into the designated layout
78111 // accepts LinearLayout to be placed into, context for fileInput, view for snackbar
79112 // returns void
80113 public static void readFile (LinearLayout emoteListLayout , String fileName , final Context context , Activity activity , final View baseView ) {
81- File internalStorageDir = context .getFilesDir ();
82114 try {
83115 // instantiate fis
84116 FileInputStream fileInputStream = context .openFileInput (fileName );
0 commit comments