Skip to content

Commit

Permalink
Merge branch 'skip_login' of github.com:ujjwalagrawal17/apps-android-…
Browse files Browse the repository at this point in the history
…commons
  • Loading branch information
nicolas-raoul committed Aug 7, 2018
2 parents 23014e0 + bf58fca commit f12f1d5
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 13 deletions.
31 changes: 31 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/auth/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.NavUtils;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDelegate;
import android.text.Editable;
import android.text.TextWatcher;
Expand All @@ -26,6 +27,7 @@
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.Locale;
Expand All @@ -41,6 +43,7 @@
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.WelcomeActivity;
import fr.free.nrw.commons.category.CategoryImagesActivity;
import fr.free.nrw.commons.contributions.ContributionsActivity;
import fr.free.nrw.commons.di.ApplicationlessInjection;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
Expand All @@ -60,6 +63,7 @@
public class LoginActivity extends AccountAuthenticatorActivity {

public static final String PARAM_USERNAME = "fr.free.nrw.commons.login.username";
private static final String FEATURED_IMAGES_CATEGORY = "Category:Featured_pictures_on_Wikimedia_Commons";

@Inject MediaWikiApi mwApi;
@Inject SessionManager sessionManager;
Expand All @@ -76,6 +80,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
@BindView(R.id.login_credentials) TextView loginCredentials;
@BindView(R.id.two_factor_container) TextInputLayout twoFactorContainer;
@BindView(R.id.forgotPassword) HtmlTextView forgotPasswordText;
@BindView(R.id.skipLogin) HtmlTextView skipLoginText;

ProgressDialog progressDialog;
private AppCompatDelegate delegate;
Expand Down Expand Up @@ -125,6 +130,15 @@ public void onCreate(Bundle savedInstanceState) {
signupButton.setOnClickListener(view -> signUp());

forgotPasswordText.setOnClickListener(view -> forgotPassword());
skipLoginText.setOnClickListener(view -> new AlertDialog.Builder(this).setTitle(R.string.skip_login_title)
.setMessage(R.string.skip_login_message)
.setCancelable(false)
.setPositiveButton(R.string.yes, (dialog, which) -> {
dialog.cancel();
skipLogin();
})
.setNegativeButton(R.string.no, (dialog, which) -> dialog.cancel())
.show());

if(BuildConfig.FLAVOR.equals("beta")){
loginCredentials.setText(getString(R.string.login_credential));
Expand All @@ -133,6 +147,17 @@ public void onCreate(Bundle savedInstanceState) {
}
}

/**
* This function is called when user skips the login.
* It redirects the user to Explore Activity.
*/
private void skipLogin() {
prefs.edit().putBoolean("login_skipped", true).apply();
CategoryImagesActivity.startYourself(this, getString(R.string.title_activity_explore), FEATURED_IMAGES_CATEGORY);
finish();

}

private void forgotPassword() {
Utils.handleWebUrl(this, Uri.parse(BuildConfig.FORGOT_PASSWORD_URL));
}
Expand All @@ -159,9 +184,15 @@ protected void onResume() {
if (sessionManager.getCurrentAccount() != null
&& sessionManager.isUserLoggedIn()
&& sessionManager.getCachedAuthCookie() != null) {
prefs.edit().putBoolean("login_skipped", false).apply();
sessionManager.revalidateAuthToken();
startMainActivity();
}

if (prefs.getBoolean("login_skipped", false)){
skipLogin();
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand Down Expand Up @@ -51,8 +50,10 @@
import uk.co.deanwild.materialshowcaseview.IShowcaseListener;
import uk.co.deanwild.materialshowcaseview.MaterialShowcaseView;

import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.*;
import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED;
import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.LOCATION_SLIGHTLY_CHANGED;
import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.MAP_UPDATED;
import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.PERMISSION_JUST_GRANTED;


public class NearbyActivity extends NavigationBaseActivity implements LocationUpdateListener,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -53,8 +54,10 @@
import javax.inject.Named;

import dagger.android.support.DaggerFragment;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.auth.LoginActivity;
import fr.free.nrw.commons.contributions.ContributionController;
import fr.free.nrw.commons.utils.ContributionUtils;
import fr.free.nrw.commons.utils.UriDeserializer;
Expand All @@ -68,6 +71,8 @@

public class NearbyMapFragment extends DaggerFragment {

@Inject
@Named("application_preferences") SharedPreferences applicationPrefs;
public MapView mapView;
private List<NearbyBaseMarker> baseMarkerOptions;
private fr.free.nrw.commons.location.LatLng curLatLng;
Expand Down Expand Up @@ -373,7 +378,23 @@ private void initViews() {
}

private void setListeners() {
fabPlus.setOnClickListener(view -> animateFAB(isFabOpen));
fabPlus.setOnClickListener(view -> {
if (applicationPrefs.getBoolean("login_skipped", true)) {
// prompt the user to login
new AlertDialog.Builder(getContext())
.setMessage(R.string.login_alert_message)
.setPositiveButton(R.string.login, (dialog, which) -> {
// logout of the app
BaseLogoutListener logoutListener = new BaseLogoutListener();
CommonsApplication app = (CommonsApplication) getActivity().getApplication();
app.clearApplicationData(getContext(), logoutListener);

})
.show();
}else {
animateFAB(isFabOpen);
}
});

bottomSheetDetails.setOnClickListener(view -> {
if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
Expand Down Expand Up @@ -491,6 +512,21 @@ public void onMapReady(MapboxMap mapboxMap) {
mapView.setStyleUrl("asset://mapstyle.json");
}

/**
* onLogoutComplete is called after shared preferences and data stored in local database are cleared.
*/
private class BaseLogoutListener implements CommonsApplication.LogoutListener {
@Override
public void onLogoutComplete() {
Timber.d("Logout complete callback received.");
Intent nearbyIntent = new Intent( getActivity(), LoginActivity.class);
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(nearbyIntent);
getActivity().finish();
}
}

/**
* Adds a marker for the user's current position. Adds a
* circle which uses the accuracy * 2, to draw a circle
Expand Down
54 changes: 44 additions & 10 deletions app/src/main/java/fr/free/nrw/commons/nearby/PlaceRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.transition.TransitionManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.LayoutInflater;
Expand All @@ -27,12 +28,17 @@
import butterknife.ButterKnife;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.auth.LoginActivity;
import fr.free.nrw.commons.contributions.ContributionController;
import fr.free.nrw.commons.di.ApplicationlessInjection;
import timber.log.Timber;

import static fr.free.nrw.commons.theme.NavigationBaseActivity.startActivityWithFlags;

public class PlaceRenderer extends Renderer<Place> {

@Inject
@Named("application_preferences") SharedPreferences applicationPrefs;
@BindView(R.id.tvName) TextView tvName;
@BindView(R.id.tvDesc) TextView tvDesc;
@BindView(R.id.distance) TextView distance;
Expand Down Expand Up @@ -88,9 +94,9 @@ protected void hookListeners(View view) {
Log.d("Renderer", "clicked");
TransitionManager.beginDelayedTransition(buttonLayout);

if(buttonLayout.isShown()){
if (buttonLayout.isShown()) {
closeLayout(buttonLayout);
}else {
} else {
openLayout(buttonLayout);
}

Expand All @@ -106,18 +112,46 @@ protected void hookListeners(View view) {
});

cameraButton.setOnClickListener(view2 -> {
Timber.d("Camera button tapped. Image title: " + place.getName() + "Image desc: " + place.getLongDescription());
DirectUpload directUpload = new DirectUpload(fragment, controller);
storeSharedPrefs();
directUpload.initiateCameraUpload();
if (applicationPrefs.getBoolean("login_skipped", true)) {
// prompt the user to login
new AlertDialog.Builder(getContext())
.setMessage(R.string.login_alert_message)
.setPositiveButton(R.string.login, (dialog, which) -> {
startActivityWithFlags( getContext(), LoginActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TOP,
Intent.FLAG_ACTIVITY_SINGLE_TOP);
prefs.edit().putBoolean("login_skipped", false).apply();
fragment.getActivity().finish();
})
.show();
} else {
Timber.d("Camera button tapped. Image title: " + place.getName() + "Image desc: " + place.getLongDescription());
DirectUpload directUpload = new DirectUpload(fragment, controller);
storeSharedPrefs();
directUpload.initiateCameraUpload();
}
});


galleryButton.setOnClickListener(view3 -> {
Timber.d("Gallery button tapped. Image title: " + place.getName() + "Image desc: " + place.getLongDescription());
DirectUpload directUpload = new DirectUpload(fragment, controller);
storeSharedPrefs();
directUpload.initiateGalleryUpload();
if (applicationPrefs.getBoolean("login_skipped", true)) {
// prompt the user to login
new AlertDialog.Builder(getContext())
.setMessage(R.string.login_alert_message)
.setPositiveButton(R.string.login, (dialog, which) -> {
startActivityWithFlags( getContext(), LoginActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TOP,
Intent.FLAG_ACTIVITY_SINGLE_TOP);
prefs.edit().putBoolean("login_skipped", false).apply();
fragment.getActivity().finish();
})
.show();
}else {
Timber.d("Gallery button tapped. Image title: " + place.getName() + "Image desc: " + place.getLongDescription());
DirectUpload directUpload = new DirectUpload(fragment, controller);
storeSharedPrefs();
directUpload.initiateGalleryUpload();
}
});

}

private void storeSharedPrefs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import fr.free.nrw.commons.AboutActivity;
import fr.free.nrw.commons.BuildConfig;
Expand Down Expand Up @@ -46,6 +51,8 @@ public abstract class NavigationBaseActivity extends BaseActivity
NavigationView navigationView;
@BindView(R.id.drawer_layout)
DrawerLayout drawerLayout;
@Inject @Named("application_preferences") SharedPreferences prefs;


private ActionBarDrawerToggle toggle;

Expand All @@ -61,6 +68,24 @@ public void initDrawer() {
toggle.syncState();
setDrawerPaneWidth();
setUserName();
Menu nav_Menu = navigationView.getMenu();
View headerLayout = navigationView.getHeaderView(0);
ImageView userIcon = headerLayout.findViewById(R.id.user_icon);
if (prefs.getBoolean("login_skipped", false)) {
userIcon.setVisibility(View.GONE);
nav_Menu.findItem(R.id.action_login).setVisible(true);
nav_Menu.findItem(R.id.action_home).setVisible(false);
nav_Menu.findItem(R.id.action_notifications).setVisible(false);
nav_Menu.findItem(R.id.action_settings).setVisible(false);
nav_Menu.findItem(R.id.action_logout).setVisible(false);
}else {
userIcon.setVisibility(View.VISIBLE);
nav_Menu.findItem(R.id.action_login).setVisible(false);
nav_Menu.findItem(R.id.action_home).setVisible(true);
nav_Menu.findItem(R.id.action_notifications).setVisible(true);
nav_Menu.findItem(R.id.action_settings).setVisible(true);
nav_Menu.findItem(R.id.action_logout).setVisible(true);
}
}

/**
Expand Down Expand Up @@ -120,6 +145,14 @@ private void setDrawerPaneWidth() {
public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
final int itemId = item.getItemId();
switch (itemId) {
case R.id.action_login:
drawerLayout.closeDrawer(navigationView);
startActivityWithFlags(
this, LoginActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TOP,
Intent.FLAG_ACTIVITY_SINGLE_TOP);
prefs.edit().putBoolean("login_skipped", false).apply();
finish();
return true;
case R.id.action_home:
drawerLayout.closeDrawer(navigationView);
startActivityWithFlags(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
Expand Down Expand Up @@ -103,6 +104,7 @@ public class ShareActivity
ModifierSequenceDao modifierSequenceDao;
@Inject
CategoryApi apiCall;
@Inject @Named("application_preferences") SharedPreferences applicationPrefs;
@Inject
@Named("default_preferences")
SharedPreferences prefs;
Expand Down Expand Up @@ -341,6 +343,12 @@ private void createContributionWithReceivedIntent(Bundle savedInstanceState) {
checkIfFileExists();
gpsObj = fileObj.processFileCoordinates(locationPermitted);
decimalCoords = fileObj.getDecimalCoords();
if (sessionManager.getCurrentAccount() == null) {
Toast.makeText(this, getString(R.string.login_alert_message), Toast.LENGTH_SHORT).show();
applicationPrefs.edit().putBoolean("login_skipped", false).apply();
Intent loginIntent = new Intent(ShareActivity.this, LoginActivity.class);
startActivity(loginIntent);
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@
android:layout_marginBottom="@dimen/standard_gap"
android:text="@string/forgot_password" />

<fr.free.nrw.commons.ui.widget.HtmlTextView
android:visibility="visible"
android:id="@+id/skipLogin"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_below="@id/forgotPassword"
android:layout_marginBottom="@dimen/standard_gap"
android:text="@string/skip_login" />

</RelativeLayout>

</android.support.v7.widget.CardView>
Expand Down
Loading

0 comments on commit f12f1d5

Please sign in to comment.