Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Fix-267: User Image feature added #136

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 203 additions & 14 deletions app/src/main/java/org/apache/fineract/ui/online/DashboardActivity.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,72 @@
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;
import org.apache.fineract.jobs.StartSyncJob;
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;
import org.apache.fineract.ui.online.launcher.LauncherActivity;
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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
8 changes: 4 additions & 4 deletions app/src/main/res/layout/layout_nav_header.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
android:id="@+id/imageView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/nav_user_image"
android:layout_height="80dp"
android:layout_width="80dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@android:drawable/sym_def_app_icon"/>
app:srcCompat="@drawable/mifos_logo_new"/>

<TextView
android:layout_height="wrap_content"
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,14 @@
<string name="please_wait_updating_group_status">"Updating group status, please wait... "</string>
<string name="error_while_updating_group_status">Error while updating group status</string>
<string name="error_while_creating_group">Error while creating group</string>

<!--User Image related strings -->
<string name="user_take_photo">Take Photo</string>
<string name="user_gallery">Choose from Gallery</string>
<string name="user_remove_image">Remove image</string>
<string name="user_removed_image_success">Image removed successfully</string>
<string name="user_image_not_exist">Image doesn\'t exist on device</string>
<string name="user_image_update_success">Image updated successfully</string>
<string name="user_image_size">Please select image with smaller size</string>
<string name="user_image_error">Some error occurred in selecting image</string>
</resources>