diff --git a/app/src/main/java/org/apache/fineract/ui/online/DashboardActivity.java b/app/src/main/java/org/apache/fineract/ui/online/DashboardActivity.java index 7f0460d9..a4b1f723 100644 --- a/app/src/main/java/org/apache/fineract/ui/online/DashboardActivity.java +++ b/app/src/main/java/org/apache/fineract/ui/online/DashboardActivity.java @@ -1,22 +1,30 @@ package org.apache.fineract.ui.online; +import android.Manifest; +import android.app.AlertDialog; +import android.content.ContextWrapper; import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.view.MenuItem; +import android.view.View; +import android.widget.ImageView; +import android.widget.Toast; import androidx.annotation.NonNull; - -import com.google.android.material.navigation.NavigationView; -import com.google.android.material.snackbar.Snackbar; - +import androidx.annotation.RequiresApi; +import androidx.appcompat.app.ActionBarDrawerToggle; +import androidx.core.app.ActivityCompat; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; -import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.fragment.app.Fragment; -import android.view.MenuItem; -import android.view.View; -import android.widget.Toast; +import com.google.android.material.navigation.NavigationView; +import com.google.android.material.snackbar.Snackbar; import org.apache.fineract.R; import org.apache.fineract.data.local.PreferencesHelper; @@ -24,8 +32,8 @@ import org.apache.fineract.ui.base.FineractBaseActivity; import org.apache.fineract.ui.base.Toaster; import org.apache.fineract.ui.offline.CustomerPayloadFragment; -import org.apache.fineract.ui.online.accounting.ledgers.LedgerFragment; import org.apache.fineract.ui.online.accounting.accounts.AccountsFragment; +import org.apache.fineract.ui.online.accounting.ledgers.LedgerFragment; import org.apache.fineract.ui.online.customers.customerlist.CustomersFragment; import org.apache.fineract.ui.online.dashboard.DashboardFragment; import org.apache.fineract.ui.online.groups.grouplist.GroupListFragment; @@ -33,21 +41,32 @@ import org.apache.fineract.ui.online.roles.roleslist.RolesFragment; import org.apache.fineract.ui.online.teller.TellerFragment; import org.apache.fineract.ui.product.ProductFragment; +import org.apache.fineract.utils.CheckSelfPermissionAndRequest; +import org.apache.fineract.utils.ConstantKeys; import org.apache.fineract.utils.MaterialDialog; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.logging.Logger; + import javax.inject.Inject; import butterknife.BindView; import butterknife.ButterKnife; /** - * @author Rajan Maurya - * On 19/06/17. + * @author Rajan Maurya On 19/06/17. */ public class DashboardActivity extends FineractBaseActivity implements NavigationView.OnNavigationItemSelectedListener { public static final String LOG_TAG = DashboardActivity.class.getSimpleName(); + public static final int REQUEST_PERMISSIONS = 0; + public static final int REQUEST_IMAGE_CAPTURE = 1; + public static final int REQUEST_PHOTO_FROM_GALLERY = 2; @BindView(R.id.nav_view) NavigationView navigationView; @@ -59,6 +78,7 @@ public class DashboardActivity extends FineractBaseActivity implements PreferencesHelper preferencesHelper; private boolean isBackPressedOnce = false; + private ImageView userImage; @Override protected void onCreate(Bundle savedInstanceState) { @@ -80,6 +100,10 @@ protected void onCreate(Bundle savedInstanceState) { public void setupNavigationBar() { navigationView.setNavigationItemSelectedListener(this); + userImage = navigationView.getHeaderView(0).findViewById(R.id.nav_user_image); + loadUserImage(); + userImage.setOnClickListener(v -> selectImage()); + // setup drawer layout and sync to toolbar ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer) { @@ -183,6 +207,7 @@ public void logout() { .setPositiveButton(getString(R.string.dialog_action_logout), (dialog, which) -> { preferencesHelper.clear(); + removeImage(); Intent intent = new Intent(DashboardActivity.this, LauncherActivity.class); startActivity(intent); @@ -197,12 +222,176 @@ public void logout() { .show(); } + public void setNavigationViewSelectedItem(int id) { + navigationView.setCheckedItem(id); + } + + private void selectImage() { + if (CheckSelfPermissionAndRequest.checkMultiplePermissions(getThemedContext(), + Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA) && + CheckSelfPermissionAndRequest.checkSelfPermission(getThemedContext(), + Manifest.permission.READ_EXTERNAL_STORAGE)) { + showsUserImageOptions(); + } else { + requestRWCPermissions(); + } + } + + //RWC- read, write and camera + private void requestRWCPermissions() { + ActivityCompat.requestPermissions(DashboardActivity.this, + new String[]{ + Manifest.permission.WRITE_EXTERNAL_STORAGE, + Manifest.permission.CAMERA, + ConstantKeys.PERMISSIONS_WRITE_EXTERNAL_STORAGE_STATUS, + ConstantKeys.PERMISSIONS_CAMERA_STATUS}, REQUEST_PERMISSIONS); + } + + @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { + protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); + if (resultCode != RESULT_CANCELED) { + switch (requestCode) { + + case REQUEST_PERMISSIONS: + showsUserImageOptions(); + break; + + case REQUEST_IMAGE_CAPTURE: + if (resultCode == RESULT_OK && data != null) { + Bitmap capturedImage = (Bitmap) data.getExtras().get("data"); + File file = saveImage(capturedImage); + if (showImageSizeExceededOrNot(file)) { + userImage.setImageBitmap(capturedImage); + Toaster.show(navigationView, + getString(R.string.user_image_update_success)); + } else { + file.delete(); + Toaster.show(navigationView, getString(R.string.user_image_size)); + } + } + break; + + case REQUEST_PHOTO_FROM_GALLERY: + if (resultCode == RESULT_OK && data != null) { + try { + Uri imageUri = data.getData(); + InputStream imageStream = getContentResolver().openInputStream( + imageUri); + Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); + + File file = saveImage(selectedImage); + if (showImageSizeExceededOrNot(file)) { + userImage.setImageBitmap(selectedImage); + Toaster.show(navigationView, + getString(R.string.user_image_update_success)); + } else { + file.delete(); + Toaster.show(navigationView, + getString(R.string.user_image_size)); + } + } catch (FileNotFoundException e) { + Logger.getLogger(e.getMessage()); + Toaster.show(navigationView, getString(R.string.user_image_error)); + } + } + break; + } + } } - public void setNavigationViewSelectedItem(int id) { - navigationView.setCheckedItem(id); + public boolean showImageSizeExceededOrNot(File file) { + int fileSize = (int) (file.length() / 1024); + return fileSize < 1024; + } + + public void showsUserImageOptions() { + CharSequence[] options = + {getString(R.string.user_take_photo), getString(R.string.user_gallery), + getString( + R.string.user_remove_image), getString(R.string.cancel)}; + + AlertDialog.Builder builder = new AlertDialog.Builder(getThemedContext()); + builder.setItems(options, (dialog, item) -> { + + if (options[item].equals(getString(R.string.user_take_photo))) { + Intent takePicture = new Intent( + android.provider.MediaStore.ACTION_IMAGE_CAPTURE); + startActivityForResult(takePicture, REQUEST_IMAGE_CAPTURE); + + } else if (options[item].equals(getString(R.string.user_gallery))) { + Intent intentDocument = new Intent(Intent.ACTION_GET_CONTENT); + intentDocument.setType("image/*"); + startActivityForResult(intentDocument, REQUEST_PHOTO_FROM_GALLERY); + + } else if (options[item].equals(getString(R.string.user_remove_image))) { + + if (removeImage()) { + userImage.setImageDrawable( + getResources().getDrawable(R.drawable.mifos_logo_new)); + Toaster.show(navigationView, + getString(R.string.user_removed_image_success)); + } else { + Toaster.show(navigationView, getString(R.string.user_image_not_exist)); + } + + } else if (options[item].equals(getString(R.string.cancel))) { + dialog.dismiss(); + } + }); + builder.show(); + } + + public void loadUserImage() { + ContextWrapper cw = new ContextWrapper(getThemedContext()); + File dir = cw.getDir(getString(R.string.fineract), MODE_PRIVATE); + File file = new File(dir, preferencesHelper.getUserName() + ".png"); + if (file.exists()) { + userImage.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath())); + } else { + userImage.setImageDrawable(getResources().getDrawable(R.drawable.mifos_logo_new)); + } + } + + public boolean removeImage() { + ContextWrapper cw = new ContextWrapper(getThemedContext()); + File dir = cw.getDir(getString(R.string.fineract), MODE_PRIVATE); + File file = new File(dir, preferencesHelper.getUserName() + ".png"); + if (file.exists()) { + return file.delete(); + } + return false; + } + + @RequiresApi(api = Build.VERSION_CODES.KITKAT) + public File saveImage(Bitmap bitmapImage) { + ContextWrapper cw = new ContextWrapper(getThemedContext()); + File dir = cw.getDir(getString(R.string.fineract), MODE_PRIVATE); + File file = new File(dir, preferencesHelper.getUserName() + ".png"); + FileOutputStream fos = null; + int ratio = 100; + if (bitmapImage.getAllocationByteCount() < 1000) { + ratio = 65; + } else if (bitmapImage.getAllocationByteCount() <= 5000) { + ratio = 10; + } else if (bitmapImage.getAllocationByteCount() <= 7000) { + ratio = 2; + } else { + ratio = 1; + } + try { + fos = new FileOutputStream(file); + bitmapImage.compress(Bitmap.CompressFormat.PNG, ratio, fos); + } catch (Exception e) { + Logger.getLogger(e.getMessage()); + } finally { + try { + fos.close(); + } catch (IOException e) { + Logger.getLogger(e.getMessage()); + } + } + return file; } } diff --git a/app/src/main/res/layout/layout_nav_header.xml b/app/src/main/res/layout/layout_nav_header.xml index b7e4ac87..bb790d94 100644 --- a/app/src/main/res/layout/layout_nav_header.xml +++ b/app/src/main/res/layout/layout_nav_header.xml @@ -14,11 +14,11 @@ android:theme="@style/ThemeOverlay.AppCompat.Dark"> + app:srcCompat="@drawable/mifos_logo_new"/> "Updating group status, please wait... " Error while updating group status Error while creating group + + + Take Photo + Choose from Gallery + Remove image + Image removed successfully + Image doesn\'t exist on device + Image updated successfully + Please select image with smaller size + Some error occurred in selecting image