@ -1,12 +1,15 @@
package com.fox2code.mmm ;
package com.fox2code.mmm ;
import android.annotation.SuppressLint ;
import android.content.Context ;
import android.content.Intent ;
import android.content.Intent ;
import android.net.Uri ;
import android.net.Uri ;
import android.os.Build ;
import android.os.Build ;
import android.os.Bundle ;
import android.os.Bundle ;
import androidx. appcompat.app.AppCompatActivity ;
import androidx. core.content.FileProvider ;
import com.fox2code.foxcompat.app.FoxActivity ;
import com.fox2code.mmm.utils.io.Http ;
import com.fox2code.mmm.utils.io.Http ;
import com.google.android.material.button.MaterialButton ;
import com.google.android.material.button.MaterialButton ;
import com.google.android.material.progressindicator.LinearProgressIndicator ;
import com.google.android.material.progressindicator.LinearProgressIndicator ;
@ -21,8 +24,10 @@ import java.io.FileOutputStream;
import java.io.IOException ;
import java.io.IOException ;
import java.util.Objects ;
import java.util.Objects ;
import timber.log.Timber ;
@SuppressWarnings ( "UnnecessaryReturnStatement" )
@SuppressWarnings ( "UnnecessaryReturnStatement" )
public class UpdateActivity extends AppCompatActivity {
public class UpdateActivity extends Fox Activity {
@Override
@Override
protected void onCreate ( Bundle savedInstanceState ) {
protected void onCreate ( Bundle savedInstanceState ) {
@ -36,35 +41,39 @@ public class UpdateActivity extends AppCompatActivity {
// set status text to please wait
// set status text to please wait
statusTextView . setText ( R . string . please_wait ) ;
statusTextView . setText ( R . string . please_wait ) ;
// for debug builds, set update_debug_warning to visible and return
// for debug builds, set update_debug_warning to visible and return
if ( BuildConfig . DEBUG ) {
/ * * if ( BuildConfig . DEBUG ) {
findViewById ( R . id . update_debug_warning ) . setVisibility ( MaterialTextView . VISIBLE ) ;
findViewById ( R . id . update_debug_warning ) . setVisibility ( MaterialTextView . VISIBLE ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
statusTextView . setVisibility ( MaterialTextView . INVISIBLE ) ;
statusTextView . setVisibility ( MaterialTextView . INVISIBLE ) ;
return ;
return ;
}
} * /
new Thread ( ( ) - > {
// Now, parse the intent
// Now, parse the intent
Bundle extras = getIntent ( ) . getExtras ( ) ;
String extras = getIntent ( ) . getAction ( ) ;
// if extras is null, then we are in a bad state or user launched the activity manually
// if extras is null, then we are in a bad state or user launched the activity manually
if ( extras = = null ) {
if ( extras = = null ) {
runOnUiThread ( ( ) - > {
// set status text to error
// set status text to error
statusTextView . setText ( R . string . error_no_extras ) ;
statusTextView . setText ( R . string . error_no_extras ) ;
// set progress bar to error
// set progress bar to error
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
// return
} ) ;
return ;
return ;
}
}
// get action
// get action
ACTIONS action = ACTIONS . valueOf ( extras . getString ( "action" ) ) ;
ACTIONS action = ACTIONS . valueOf ( extras ) ;
// if action is null, then we are in a bad state or user launched the activity manually
// if action is null, then we are in a bad state or user launched the activity manually
if ( Objects . isNull ( action ) ) {
if ( Objects . isNull ( action ) ) {
runOnUiThread ( ( ) - > {
// set status text to error
// set status text to error
statusTextView . setText ( R . string . error_no_action ) ;
statusTextView . setText ( R . string . error_no_action ) ;
// set progress bar to error
// set progress bar to error
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
} ) ;
// return
// return
return ;
return ;
}
}
@ -75,42 +84,50 @@ public class UpdateActivity extends AppCompatActivity {
} else if ( action = = ACTIONS . DOWNLOAD ) {
} else if ( action = = ACTIONS . DOWNLOAD ) {
try {
try {
downloadUpdate ( ) ;
downloadUpdate ( ) ;
} catch ( JSONException e ) {
} catch (
JSONException e ) {
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
runOnUiThread ( ( ) - > {
// set status text to error
// set status text to error
statusTextView . setText ( R . string . error_download_update ) ;
statusTextView . setText ( R . string . error_download_update ) ;
// set progress bar to error
// set progress bar to error
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
} ) ;
}
}
} else if ( action = = ACTIONS . INSTALL ) {
} else if ( action = = ACTIONS . INSTALL ) {
// ensure path was passed and points to a file within our cache directory
// ensure path was passed and points to a file within our cache directory
String path = extras . getString ( "path" ) ;
String path = getIntent ( ) . getStringExtra ( "path" ) ;
if ( path = = null ) {
if ( path = = null ) {
runOnUiThread ( ( ) - > {
// set status text to error
// set status text to error
statusTextView . setText ( R . string . no_file_found ) ;
statusTextView . setText ( R . string . no_file_found ) ;
// set progress bar to error
// set progress bar to error
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
// return
} ) ;
return ;
return ;
}
}
File file = new File ( path ) ;
File file = new File ( path ) ;
if ( ! file . exists ( ) ) {
if ( ! file . exists ( ) ) {
runOnUiThread ( ( ) - > {
// set status text to error
// set status text to error
statusTextView . setText ( R . string . no_file_found ) ;
statusTextView . setText ( R . string . no_file_found ) ;
// set progress bar to error
// set progress bar to error
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
} ) ;
// return
// return
return ;
return ;
}
}
if ( ! Objects . equals ( file . getParentFile ( ) , getCacheDir ( ) ) ) {
if ( ! Objects . equals ( file . getParentFile ( ) , getCacheDir ( ) ) ) {
// set status text to error
// set status text to error
runOnUiThread ( ( ) - > {
statusTextView . setText ( R . string . no_file_found ) ;
statusTextView . setText ( R . string . no_file_found ) ;
// set progress bar to error
// set progress bar to error
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
} ) ;
// return
// return
return ;
return ;
}
}
@ -121,6 +138,8 @@ public class UpdateActivity extends AppCompatActivity {
// install update
// install update
installUpdate ( file ) ;
installUpdate ( file ) ;
}
}
} ) . start ( ) ;
}
}
public void checkForUpdate ( ) {
public void checkForUpdate ( ) {
@ -163,7 +182,11 @@ public class UpdateActivity extends AppCompatActivity {
lastestJSON = Http . doHttpGet ( AppUpdateManager . RELEASES_API_URL , false ) ;
lastestJSON = Http . doHttpGet ( AppUpdateManager . RELEASES_API_URL , false ) ;
} catch (
} catch (
Exception e ) {
Exception e ) {
e . printStackTrace ( ) ;
// when logging, REMOVE the json from the log
String msg = e . getMessage ( ) ;
// remove everything from the first { to the last }
msg = Objects . requireNonNull ( msg ) . substring ( 0 , msg . indexOf ( "{" ) ) + msg . substring ( msg . lastIndexOf ( "}" ) + 1 ) ;
Timber . e ( msg ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
statusTextView . setText ( R . string . error_download_update ) ;
statusTextView . setText ( R . string . error_download_update ) ;
@ -174,6 +197,8 @@ public class UpdateActivity extends AppCompatActivity {
JSONArray assets = latestJSON . getJSONArray ( "assets" ) ;
JSONArray assets = latestJSON . getJSONArray ( "assets" ) ;
// get the asset we want
// get the asset we want
JSONObject asset = null ;
JSONObject asset = null ;
// iterate through assets until we find the one that contains Build.SUPPORTED_ABIS[0]
while ( Objects . isNull ( asset ) ) {
for ( int i = 0 ; i < assets . length ( ) ; i + + ) {
for ( int i = 0 ; i < assets . length ( ) ; i + + ) {
JSONObject asset1 = assets . getJSONObject ( i ) ;
JSONObject asset1 = assets . getJSONObject ( i ) ;
if ( asset1 . getString ( "name" ) . contains ( Build . SUPPORTED_ABIS [ 0 ] ) ) {
if ( asset1 . getString ( "name" ) . contains ( Build . SUPPORTED_ABIS [ 0 ] ) ) {
@ -181,6 +206,7 @@ public class UpdateActivity extends AppCompatActivity {
break ;
break ;
}
}
}
}
}
// if asset is null, then we are in a bad state
// if asset is null, then we are in a bad state
if ( Objects . isNull ( asset ) ) {
if ( Objects . isNull ( asset ) ) {
// set status text to error
// set status text to error
@ -195,52 +221,62 @@ public class UpdateActivity extends AppCompatActivity {
String downloadUrl = Objects . requireNonNull ( asset ) . getString ( "browser_download_url" ) ;
String downloadUrl = Objects . requireNonNull ( asset ) . getString ( "browser_download_url" ) ;
// get the download size
// get the download size
long downloadSize = asset . getLong ( "size" ) ;
long downloadSize = asset . getLong ( "size" ) ;
runOnUiThread ( ( ) - > {
// set status text to downloading update
// set status text to downloading update
statusTextView . setText ( getString ( R . string . downloading_update , 0 ) ) ;
statusTextView . setText ( getString ( R . string . downloading_update , 0 ) ) ;
// set progress bar to 0
// set progress bar to 0
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
progressIndicator . setProgressCompat ( 0 , false ) ;
} ) ;
// download the update
// download the update
byte [ ] update = new byte [ 0 ] ;
byte [ ] update = new byte [ 0 ] ;
try {
try {
update = Http . doHttpGet ( downloadUrl , ( downloaded , total , done ) - > {
update = Http . doHttpGet ( downloadUrl , ( downloaded , total , done ) - > runOnUiThread ( ( ) - > {
// update progress bar
// update progress bar
progressIndicator . setProgressCompat ( ( int ) ( ( ( float ) downloaded / ( float ) total ) * 100 ) , true ) ;
progressIndicator . setProgressCompat ( ( int ) ( ( ( float ) downloaded / ( float ) total ) * 100 ) , true ) ;
// update status text
// update status text
statusTextView . setText ( getString ( R . string . downloading_update , ( int ) ( ( ( float ) downloaded / ( float ) total ) * 100 ) ) ) ;
statusTextView . setText ( getString ( R . string . downloading_update , ( int ) ( ( ( float ) downloaded / ( float ) total ) * 100 ) ) ) ;
} ) ;
} ) ) ;
} catch (
} catch (
Exception e ) {
Exception e ) {
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
runOnUiThread ( ( ) - > {
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
statusTextView . setText ( R . string . error_download_update ) ;
statusTextView . setText ( R . string . error_download_update ) ;
} ) ;
}
}
// if update is null, then we are in a bad state
// if update is null, then we are in a bad state
if ( Objects . isNull ( update ) ) {
if ( Objects . isNull ( update ) ) {
runOnUiThread ( ( ) - > {
// set status text to error
// set status text to error
statusTextView . setText ( R . string . error_download_update ) ;
statusTextView . setText ( R . string . error_download_update ) ;
// set progress bar to error
// set progress bar to error
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
} ) ;
// return
// return
return ;
return ;
}
}
// if update is not the same size as the download size, then we are in a bad state
// if update is not the same size as the download size, then we are in a bad state
if ( update . length ! = downloadSize ) {
if ( update . length ! = downloadSize ) {
runOnUiThread ( ( ) - > {
// set status text to error
// set status text to error
statusTextView . setText ( R . string . error_download_update ) ;
statusTextView . setText ( R . string . error_download_update ) ;
// set progress bar to error
// set progress bar to error
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
} ) ;
// return
// return
return ;
return ;
}
}
// set status text to installing update
// set status text to installing update
runOnUiThread ( ( ) - > {
statusTextView . setText ( R . string . installing_update ) ;
statusTextView . setText ( R . string . installing_update ) ;
// set progress bar to 100
// set progress bar to 100
progressIndicator . setIndeterminate ( true ) ;
progressIndicator . setIndeterminate ( true ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
} ) ;
// save the update to the cache
// save the update to the cache
File updateFile = null ;
File updateFile = null ;
try {
try {
@ -251,9 +287,11 @@ public class UpdateActivity extends AppCompatActivity {
} catch (
} catch (
IOException e ) {
IOException e ) {
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
runOnUiThread ( ( ) - > {
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setIndeterminate ( false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
statusTextView . setText ( R . string . error_download_update ) ;
statusTextView . setText ( R . string . error_download_update ) ;
} ) ;
}
}
// install the update
// install the update
installUpdate ( updateFile ) ;
installUpdate ( updateFile ) ;
@ -261,8 +299,10 @@ public class UpdateActivity extends AppCompatActivity {
return ;
return ;
}
}
@SuppressLint ( "RestrictedApi" )
private void installUpdate ( File updateFile ) {
private void installUpdate ( File updateFile ) {
// get status text view
// get status text view
runOnUiThread ( ( ) - > {
MaterialTextView statusTextView = findViewById ( R . id . update_progress_text ) ;
MaterialTextView statusTextView = findViewById ( R . id . update_progress_text ) ;
// set status text to installing update
// set status text to installing update
statusTextView . setText ( R . string . installing_update ) ;
statusTextView . setText ( R . string . installing_update ) ;
@ -270,10 +310,14 @@ public class UpdateActivity extends AppCompatActivity {
LinearProgressIndicator progressIndicator = findViewById ( R . id . update_progress ) ;
LinearProgressIndicator progressIndicator = findViewById ( R . id . update_progress ) ;
progressIndicator . setIndeterminate ( true ) ;
progressIndicator . setIndeterminate ( true ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
progressIndicator . setProgressCompat ( 100 , false ) ;
// install the update
} ) ;
Intent intent = new Intent ( Intent . ACTION_VIEW ) ;
// request install permissions
intent . setDataAndType ( Uri . fromFile ( updateFile ) , "application/vnd.android.package-archive" ) ;
Intent intent = new Intent ( Intent . ACTION_INSTALL_PACKAGE ) ;
intent . setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK ) ;
Context context = getApplicationContext ( ) ;
Uri uri = FileProvider . getUriForFile ( context , context . getPackageName ( ) + ".file-provider" , updateFile ) ;
intent . setData ( uri ) ;
intent . setFlags ( Intent . FLAG_GRANT_READ_URI_PERMISSION ) ;
intent . addFlags ( Intent . FLAG_ACTIVITY_NEW_TASK ) ;
startActivity ( intent ) ;
startActivity ( intent ) ;
// return
// return
return ;
return ;