2018-01-26 02:48:04 +00:00
package cy.agorise.crystalwallet.fragments ;
2018-09-14 06:04:10 +00:00
import android.app.Activity ;
2018-01-26 02:48:04 +00:00
import android.app.Dialog ;
2018-09-14 06:04:10 +00:00
import android.arch.lifecycle.ViewModelProviders ;
2018-04-05 01:09:58 +00:00
import android.content.DialogInterface ;
2018-04-04 00:50:44 +00:00
import android.content.Intent ;
2018-09-30 05:12:40 +00:00
import android.content.pm.PackageManager ;
2018-04-04 00:50:44 +00:00
import android.net.Uri ;
2018-09-30 05:12:40 +00:00
import android.os.Build ;
2018-01-26 02:48:04 +00:00
import android.os.Bundle ;
import android.support.annotation.NonNull ;
2018-09-30 05:12:40 +00:00
import android.support.v4.app.ActivityCompat ;
2018-01-26 02:48:04 +00:00
import android.support.v4.app.DialogFragment ;
2018-09-14 06:04:10 +00:00
import android.support.v4.app.FragmentActivity ;
2018-09-30 05:12:40 +00:00
import android.support.v4.content.ContextCompat ;
2018-01-26 02:48:04 +00:00
import android.support.v7.app.AlertDialog ;
2018-09-30 05:12:40 +00:00
import android.util.Log ;
2018-01-26 02:48:04 +00:00
import android.view.LayoutInflater ;
import android.view.View ;
import android.view.ViewGroup ;
import android.view.Window ;
import android.widget.Button ;
2018-04-05 01:09:58 +00:00
import android.widget.EditText ;
2018-04-04 00:50:44 +00:00
import android.widget.Toast ;
2018-09-30 05:12:40 +00:00
import com.vincent.filepicker.ToastUtil ;
2018-04-04 00:50:44 +00:00
import java.net.URISyntaxException ;
2018-01-26 02:48:04 +00:00
import butterknife.BindView ;
import butterknife.ButterKnife ;
import butterknife.OnClick ;
import cy.agorise.crystalwallet.R ;
2018-09-14 06:04:10 +00:00
import cy.agorise.crystalwallet.activities.BoardActivity ;
2018-09-07 02:13:28 +00:00
import cy.agorise.crystalwallet.activities.ImportSeedActivity ;
2018-09-14 06:04:10 +00:00
import cy.agorise.crystalwallet.dialogs.material.CrystalDialog ;
2018-04-04 00:50:44 +00:00
import cy.agorise.crystalwallet.requestmanagers.FileServiceRequestListener ;
import cy.agorise.crystalwallet.requestmanagers.FileServiceRequests ;
import cy.agorise.crystalwallet.requestmanagers.ImportBackupRequest ;
import cy.agorise.crystalwallet.util.UriTranslator ;
2018-09-14 06:04:10 +00:00
import cy.agorise.crystalwallet.viewmodels.AccountSeedListViewModel ;
2018-01-26 02:48:04 +00:00
2018-09-07 01:08:59 +00:00
import static android.app.Activity.RESULT_OK ;
2018-01-26 02:48:04 +00:00
/ * *
* Created by xd on 1 / 25 / 18.
* Shows a dialog where the user can select how to import his / her existing account
* /
public class ImportAccountOptionsFragment extends DialogFragment {
2018-04-04 00:50:44 +00:00
public static final int FILE_CONTENT_REQUEST_CODE = 0 ;
2018-01-26 02:48:04 +00:00
@BindView ( R . id . btnCancel )
Button btnClose ;
2018-04-04 00:50:44 +00:00
@BindView ( R . id . btnImportBackup )
Button btnImportBackup ;
2018-01-26 02:48:04 +00:00
2018-09-30 05:12:40 +00:00
private static final int PERMISSION_REQUEST_CODE = 1 ;
2018-09-14 06:04:10 +00:00
/ *
Dialog for loading
* /
private CrystalDialog crystalDialog ;
/ *
* Contains the activity to close in case import succed
* * /
private Activity introActivity ;
2018-01-26 02:48:04 +00:00
public ImportAccountOptionsFragment ( ) {
// Required empty public constructor
}
public static ImportAccountOptionsFragment newInstance ( ) {
ImportAccountOptionsFragment fragment = new ImportAccountOptionsFragment ( ) ;
Bundle args = new Bundle ( ) ;
fragment . setArguments ( args ) ;
return fragment ;
}
@Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
}
@NonNull
@Override
public Dialog onCreateDialog ( Bundle savedInstanceState ) {
AlertDialog . Builder builder = new AlertDialog . Builder ( getActivity ( ) ) ;
LayoutInflater inflater = getActivity ( ) . getLayoutInflater ( ) ;
View view = inflater . inflate ( R . layout . fragment_import_account_options , null ) ;
ButterKnife . bind ( this , view ) ;
return builder . setView ( view ) . create ( ) ;
}
@Override
public void onResume ( ) {
super . onResume ( ) ;
// Force dialog fragment to use the full width of the screen
Window dialogWindow = getDialog ( ) . getWindow ( ) ;
assert dialogWindow ! = null ;
dialogWindow . setLayout ( ViewGroup . LayoutParams . MATCH_PARENT , ViewGroup . LayoutParams . WRAP_CONTENT ) ;
}
@OnClick ( R . id . btnCancel )
public void cancel ( ) {
dismiss ( ) ;
}
2018-04-04 00:50:44 +00:00
@OnClick ( R . id . btnImportBackup )
public void importBackup ( ) {
2018-09-30 05:12:40 +00:00
if ( Build . VERSION . SDK_INT > = 23 ) {
if ( checkPermission ( ) ) {
Intent fileIntent = new Intent ( Intent . ACTION_OPEN_DOCUMENT ) ;
fileIntent . setType ( "*/*" ) ;
fileIntent . addCategory ( Intent . CATEGORY_OPENABLE ) ;
startActivityForResult ( fileIntent , FILE_CONTENT_REQUEST_CODE ) ;
2018-04-04 00:50:44 +00:00
2018-09-30 05:12:40 +00:00
} else {
requestPermission ( ) ; // Code for permission
}
}
else {
Intent fileIntent = new Intent ( Intent . ACTION_OPEN_DOCUMENT ) ;
fileIntent . setType ( "*/*" ) ;
fileIntent . addCategory ( Intent . CATEGORY_OPENABLE ) ;
startActivityForResult ( fileIntent , FILE_CONTENT_REQUEST_CODE ) ;
}
2018-04-04 00:50:44 +00:00
}
2018-09-07 02:13:28 +00:00
@OnClick ( R . id . btnImportSeed )
public void importSeed ( ) {
2018-09-30 05:12:40 +00:00
if ( Build . VERSION . SDK_INT > = 23 ) {
if ( checkPermission ( ) ) {
Intent intent = new Intent ( this . getActivity ( ) , ImportSeedActivity . class ) ;
startActivity ( intent ) ;
} else {
requestPermission ( ) ; // Code for permission
}
}
else {
Intent intent = new Intent ( this . getActivity ( ) , ImportSeedActivity . class ) ;
startActivity ( intent ) ;
}
}
private boolean checkPermission ( ) {
int result = ContextCompat . checkSelfPermission ( getActivity ( ) , android . Manifest . permission . WRITE_EXTERNAL_STORAGE ) ;
if ( result = = PackageManager . PERMISSION_GRANTED ) {
return true ;
} else {
return false ;
}
}
private void requestPermission ( ) {
Log . i ( "log" , "requestPermission() entered" ) ;
if ( ActivityCompat . shouldShowRequestPermissionRationale ( getActivity ( ) , android . Manifest . permission . WRITE_EXTERNAL_STORAGE ) ) {
Toast . makeText ( getActivity ( ) , "Write External Storage permission allows us to do store images. Please allow this permission in App Settings." , Toast . LENGTH_LONG ) . show ( ) ;
} else {
// ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
requestPermissions ( new String [ ] { android . Manifest . permission . WRITE_EXTERNAL_STORAGE } , PERMISSION_REQUEST_CODE ) ;
}
}
@Override
public void onRequestPermissionsResult ( int requestCode , @NonNull String [ ] permissions , @NonNull int [ ] grantResults ) {
switch ( requestCode ) {
case PERMISSION_REQUEST_CODE :
if ( grantResults . length > 0 & & grantResults [ 0 ] = = PackageManager . PERMISSION_GRANTED ) {
Intent intent = new Intent ( getActivity ( ) , ImportSeedActivity . class ) ;
startActivity ( intent ) ;
} else {
ToastUtil . getInstance ( getActivity ( ) ) . showToast ( getActivity ( ) . getString ( R . string . Permission_Denied_WRITE_EXTERNAL_STORAGE ) ) ;
}
break ;
}
2018-09-07 02:13:28 +00:00
}
2018-04-04 00:50:44 +00:00
@Override
2018-04-05 01:09:58 +00:00
public void onActivityResult ( int requestCode , int resultCode , final Intent data ) {
2018-04-04 00:50:44 +00:00
super . onActivityResult ( requestCode , resultCode , data ) ;
2018-09-07 01:08:59 +00:00
if ( ( requestCode = = FILE_CONTENT_REQUEST_CODE ) & & ( resultCode = = RESULT_OK ) ) {
2018-04-05 01:09:58 +00:00
LayoutInflater inflater = getActivity ( ) . getLayoutInflater ( ) ;
View passwordDialogView = inflater . inflate ( R . layout . dialog_password_input , null ) ;
AlertDialog . Builder alertDialogBuilder = new AlertDialog . Builder ( getContext ( ) ) ;
alertDialogBuilder . setView ( passwordDialogView ) ;
final EditText passwordInput = ( EditText ) passwordDialogView . findViewById ( R . id . etPasswordInput ) ;
alertDialogBuilder
. setCancelable ( false )
. setPositiveButton ( "Ok" ,
new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int id ) {
String passwordString = ( passwordInput . getText ( ) ) . toString ( ) ;
Uri fileUri = data . getData ( ) ;
String filePath = null ;
try {
filePath = UriTranslator . getFilePath ( getContext ( ) , fileUri ) ;
} catch ( URISyntaxException e ) {
e . printStackTrace ( ) ;
}
final ImportBackupRequest importBackupRequest = new ImportBackupRequest ( getContext ( ) , passwordString , filePath ) ;
importBackupRequest . setListener ( new FileServiceRequestListener ( ) {
@Override
public void onCarryOut ( ) {
2018-04-12 01:37:49 +00:00
getActivity ( ) . runOnUiThread ( new Runnable ( ) {
@Override
public void run ( ) {
if ( importBackupRequest . getStatus ( ) = = ImportBackupRequest . StatusCode . SUCCEEDED ) {
2018-09-14 06:04:10 +00:00
//Checks if the user has any seed created
AccountSeedListViewModel accountSeedListViewModel = ViewModelProviders . of ( ( FragmentActivity ) getContext ( ) ) . get ( AccountSeedListViewModel . class ) ;
if ( introActivity ! = null ) {
introActivity . finish ( ) ;
}
Intent intent = new Intent ( getContext ( ) , BoardActivity . class ) ;
2018-04-12 01:37:49 +00:00
startActivity ( intent ) ;
2018-09-14 06:04:10 +00:00
dismiss ( ) ;
/ *
* Hide the loading dialog
* * /
crystalDialog . dismiss ( ) ;
2018-04-12 01:37:49 +00:00
} else if ( importBackupRequest . getStatus ( ) = = ImportBackupRequest . StatusCode . FAILED ) {
2018-09-14 06:04:10 +00:00
/ *
* Hide the loading dialog
* * /
crystalDialog . dismiss ( ) ;
2018-04-12 01:37:49 +00:00
Toast toast = Toast . makeText (
getContext ( ) , "An error ocurred while restoring the backup!" , Toast . LENGTH_LONG ) ;
toast . show ( ) ;
}
}
} ) ;
2018-04-05 01:09:58 +00:00
}
} ) ;
2018-09-14 06:04:10 +00:00
/ *
2018-09-16 01:25:41 +00:00
* Show loading dialog
* * /
2018-09-14 06:04:10 +00:00
crystalDialog = new CrystalDialog ( ( Activity ) getContext ( ) ) ;
2018-09-18 17:40:49 +00:00
crystalDialog . setText ( getContext ( ) . getString ( R . string . Restoring_backup_from_file ) ) ;
2018-09-14 06:04:10 +00:00
crystalDialog . progress ( ) ;
crystalDialog . show ( ) ;
2018-09-16 01:25:41 +00:00
FileServiceRequests . getInstance ( ) . addRequest ( importBackupRequest ) ;
2018-04-05 01:09:58 +00:00
}
} )
. setNegativeButton ( "Cancel" ,
new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int id ) {
dialog . cancel ( ) ;
}
} ) ;
AlertDialog passwordDialog = alertDialogBuilder . create ( ) ;
passwordDialog . show ( ) ;
2018-04-04 00:50:44 +00:00
}
}
2018-09-14 06:04:10 +00:00
public void setIntroActivity ( Activity introActivity ) {
this . introActivity = introActivity ;
}
2018-01-26 02:48:04 +00:00
}