Browse Source

facebook login

tags/v1.0.0
Robin Thoni 7 years ago
parent
commit
a26e946520

+ 9
- 0
app/src/main/AndroidManifest.xml View File

@@ -10,6 +10,7 @@
10 10
         android:label="@string/app_name"
11 11
         android:supportsRtl="true"
12 12
         android:theme="@style/AppTheme">
13
+
13 14
         <activity
14 15
             android:name=".ui.activities.MainActivity"
15 16
             android:label="@string/app_name"
@@ -29,6 +30,14 @@
29 30
                 android:name="android.support.PARENT_ACTIVITY"
30 31
                 android:value="com.rthoni.stssaguenay.ui.activities.MainActivity"/>
31 32
         </activity>
33
+
34
+        <activity android:name="com.facebook.FacebookActivity"
35
+                  android:configChanges=
36
+                      "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
37
+                  android:theme="@android:style/Theme.Translucent.NoTitleBar"
38
+                  android:label="@string/app_name" />
39
+        <meta-data android:name="com.facebook.sdk.ApplicationId"
40
+                   android:value="@string/facebook_app_id"/>
32 41
     </application>
33 42
 
34 43
 </manifest>

BIN
app/src/main/ic_menu_logout-web.png View File


+ 26
- 0
app/src/main/java/com/rthoni/stssaguenay/business/STSBusiness.java View File

@@ -5,6 +5,7 @@ import android.content.Context;
5 5
 import com.luticate.utils.dbo.LuDataAccessConfigDbo;
6 6
 import com.rthoni.stssaguenay.dataaccess.STSDataAccess;
7 7
 import com.rthoni.stssaguenay.dbo.FavouriteStopDbo;
8
+import com.rthoni.stssaguenay.dbo.UserDbo;
8 9
 
9 10
 import org.json.JSONArray;
10 11
 import org.json.JSONException;
@@ -55,4 +56,29 @@ public class STSBusiness {
55 56
         }
56 57
         STSDataAccess.setFavouriteStopsJson(ctx, json.toString());
57 58
     }
59
+
60
+    public static UserDbo getLoggedUser(Context ctx)
61
+    {
62
+        String str = STSDataAccess.getLoggedUserJson(ctx);
63
+        if (str != null) {
64
+            UserDbo user = new UserDbo();
65
+            try {
66
+                user.fromJson(new JSONObject(str));
67
+                return user;
68
+            } catch (JSONException e) {
69
+                e.printStackTrace();
70
+            }
71
+        }
72
+        return null;
73
+    }
74
+
75
+    public static void setLoggedUser(Context ctx, UserDbo user)
76
+    {
77
+        if (user == null) {
78
+            STSDataAccess.setLoggedUserJson(ctx, null);
79
+        }
80
+        else {
81
+            STSDataAccess.setLoggedUserJson(ctx, user.toString());
82
+        }
83
+    }
58 84
 }

+ 12
- 0
app/src/main/java/com/rthoni/stssaguenay/dataaccess/STSDataAccess.java View File

@@ -13,6 +13,8 @@ public class STSDataAccess {
13 13
 
14 14
     public static String SHARED_PREF_FAVOURITE_STOPS = "favourites-stops";
15 15
 
16
+    public static String SHARED_PREF_LOGGED_USER = "logged-user";
17
+
16 18
     public static SharedPreferences getSharedPref(Context ctx)
17 19
     {
18 20
         return ctx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
@@ -27,4 +29,14 @@ public class STSDataAccess {
27 29
     {
28 30
         getSharedPref(ctx).edit().putString(SHARED_PREF_FAVOURITE_STOPS, str).apply();
29 31
     }
32
+
33
+    public static String getLoggedUserJson(Context ctx)
34
+    {
35
+        return getSharedPref(ctx).getString(SHARED_PREF_LOGGED_USER, null);
36
+    }
37
+
38
+    public static void setLoggedUserJson(Context ctx, String str)
39
+    {
40
+        getSharedPref(ctx).edit().putString(SHARED_PREF_LOGGED_USER, str).apply();
41
+    }
30 42
 }

+ 61
- 0
app/src/main/java/com/rthoni/stssaguenay/dbo/UserDbo.java View File

@@ -0,0 +1,61 @@
1
+package com.rthoni.stssaguenay.dbo;
2
+
3
+import com.luticate.utils.dbo.LuDbo;
4
+
5
+import org.json.JSONException;
6
+import org.json.JSONObject;
7
+
8
+import java.util.HashMap;
9
+
10
+/**
11
+ * Created by robin on 10/3/16.
12
+ */
13
+
14
+public class UserDbo extends LuDbo {
15
+
16
+    protected String _id;
17
+
18
+    protected String _name;
19
+
20
+    protected String _email;
21
+
22
+    @Override
23
+    public void fromJson(JSONObject json) throws JSONException {
24
+        _id = json.getString("id");
25
+        _name = json.getString("name");
26
+        _email = json.getString("email");
27
+    }
28
+
29
+    @Override
30
+    public HashMap<String, Object> toArray() {
31
+        HashMap<String, Object> map = new HashMap<>();
32
+        map.put("id", _id);
33
+        map.put("name", _name);
34
+        map.put("email", _email);
35
+        return map;
36
+    }
37
+
38
+    public String getId() {
39
+        return _id;
40
+    }
41
+
42
+    public void setId(String id) {
43
+        _id = id;
44
+    }
45
+
46
+    public String getName() {
47
+        return _name;
48
+    }
49
+
50
+    public void setName(String name) {
51
+        _name = name;
52
+    }
53
+
54
+    public String getEmail() {
55
+        return _email;
56
+    }
57
+
58
+    public void setEmail(String email) {
59
+        _email = email;
60
+    }
61
+}

+ 115
- 23
app/src/main/java/com/rthoni/stssaguenay/ui/activities/MainActivity.java View File

@@ -3,10 +3,26 @@ package com.rthoni.stssaguenay.ui.activities;
3 3
 import android.content.Intent;
4 4
 import android.os.Bundle;
5 5
 import android.support.design.widget.FloatingActionButton;
6
+import android.support.design.widget.NavigationView;
7
+import android.support.v4.widget.DrawerLayout;
8
+import android.support.v7.app.ActionBarDrawerToggle;
6 9
 import android.support.v7.app.AppCompatActivity;
7 10
 import android.support.v7.widget.Toolbar;
11
+import android.view.Menu;
12
+import android.view.MenuItem;
8 13
 import android.view.View;
9
-
14
+import android.widget.TextView;
15
+import android.widget.Toast;
16
+
17
+import com.facebook.CallbackManager;
18
+import com.facebook.FacebookCallback;
19
+import com.facebook.FacebookException;
20
+import com.facebook.FacebookSdk;
21
+import com.facebook.GraphRequest;
22
+import com.facebook.GraphResponse;
23
+import com.facebook.login.LoginManager;
24
+import com.facebook.login.LoginResult;
25
+import com.facebook.login.widget.LoginButton;
10 26
 import com.google.android.gms.ads.AdRequest;
11 27
 import com.google.android.gms.ads.AdView;
12 28
 import com.google.android.gms.ads.MobileAds;
@@ -14,6 +30,7 @@ import com.luticate.utils.business.LuRequest;
14 30
 import com.rthoni.stssaguenay.R;
15 31
 import com.rthoni.stssaguenay.business.STSBusiness;
16 32
 import com.rthoni.stssaguenay.dbo.FavouriteStopDbo;
33
+import com.rthoni.stssaguenay.dbo.UserDbo;
17 34
 import com.rthoni.stssaguenay.ui.fragments.HomeFragment;
18 35
 
19 36
 import org.json.JSONException;
@@ -24,7 +41,7 @@ import java.util.List;
24 41
 import butterknife.BindView;
25 42
 import butterknife.ButterKnife;
26 43
 
27
-public class MainActivity extends AppCompatActivity {
44
+public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
28 45
 
29 46
     public static int ADD_FAVOURITE_STOP_REQUEST_CODE = 1;
30 47
 
@@ -33,12 +50,25 @@ public class MainActivity extends AppCompatActivity {
33 50
     @BindView(R.id.fab)
34 51
     FloatingActionButton _fab;
35 52
 
53
+    @BindView(R.id.nav_view)
54
+    NavigationView _navigationView;
55
+
56
+    private LoginButton _btnLogin;
57
+
58
+    private TextView _textUserName;
59
+
60
+    private TextView _textUserEmail;
61
+
62
+    private CallbackManager _callbackManager;
63
+
36 64
     @Override
37 65
     protected void onCreate(Bundle savedInstanceState) {
38 66
         super.onCreate(savedInstanceState);
39 67
 
40 68
         LuRequest.init(this);
41 69
 
70
+        FacebookSdk.sdkInitialize(getApplicationContext());
71
+
42 72
         setContentView(R.layout.activity_main);
43 73
         ButterKnife.bind(this);
44 74
 
@@ -50,6 +80,18 @@ public class MainActivity extends AppCompatActivity {
50 80
         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
51 81
         setSupportActionBar(toolbar);
52 82
 
83
+        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
84
+        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
85
+                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
86
+        drawer.setDrawerListener(toggle);
87
+        toggle.syncState();
88
+
89
+        _navigationView.setNavigationItemSelectedListener(this);
90
+
91
+        View headerView = _navigationView.getHeaderView(0);
92
+        _textUserName = ButterKnife.findById(headerView, R.id.textUserName);
93
+        _textUserEmail = ButterKnife.findById(headerView, R.id.textUserEmail);
94
+
53 95
         _fab.setOnClickListener(new View.OnClickListener() {
54 96
             @Override
55 97
             public void onClick(View view) {
@@ -57,6 +99,45 @@ public class MainActivity extends AppCompatActivity {
57 99
             }
58 100
         });
59 101
 
102
+        _callbackManager = CallbackManager.Factory.create();
103
+        _btnLogin = ButterKnife.findById(headerView, R.id.btnLogin);
104
+        _btnLogin.setReadPermissions("email");
105
+        _btnLogin.registerCallback(_callbackManager, new FacebookCallback<LoginResult>() {
106
+            @Override
107
+            public void onSuccess(LoginResult loginResult) {
108
+                GraphRequest request = GraphRequest.newMeRequest(
109
+                        loginResult.getAccessToken(),
110
+                        new GraphRequest.GraphJSONObjectCallback() {
111
+                            @Override
112
+                            public void onCompleted(JSONObject object, GraphResponse response) {
113
+                                try {
114
+                                    UserDbo userDbo = new UserDbo();
115
+                                    userDbo.fromJson(object);
116
+                                    onUserLogged(userDbo);
117
+                                } catch (JSONException e) {
118
+                                    e.printStackTrace();
119
+                                }
120
+                            }
121
+                        });
122
+                Bundle parameters = new Bundle();
123
+                parameters.putString("fields", "id,email,name");
124
+                request.setParameters(parameters);
125
+                request.executeAsync();
126
+                loginResult.getAccessToken();
127
+            }
128
+
129
+            @Override
130
+            public void onCancel() {
131
+
132
+            }
133
+
134
+            @Override
135
+            public void onError(FacebookException error) {
136
+                Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
137
+            }
138
+        });
139
+
140
+        updateLoginState();
60 141
         goToHome();
61 142
     }
62 143
 
@@ -104,28 +185,39 @@ public class MainActivity extends AppCompatActivity {
104 185
             STSBusiness.setFavouriteStops(this, favouriteStopDbos);
105 186
             _goToHome = true;
106 187
         }
188
+        else {
189
+            _callbackManager.onActivityResult(requestCode, resultCode, data);
190
+        }
107 191
         super.onActivityResult(requestCode, resultCode, data);
108 192
     }
109 193
 
110
-    //    @Override
111
-//    public boolean onCreateOptionsMenu(Menu menu) {
112
-//        // Inflate the menu; this adds items to the action bar if it is present.
113
-//        getMenuInflater().inflate(R.menu.menu_main, menu);
114
-//        return true;
115
-//    }
116
-
117
-//    @Override
118
-//    public boolean onOptionsItemSelected(MenuItem item) {
119
-//        // Handle action bar item clicks here. The action bar will
120
-//        // automatically handle clicks on the Home/Up button, so long
121
-//        // as you specify a parent activity in AndroidManifest.xml.
122
-//        int id = item.getItemId();
123
-//
124
-//        //noinspection SimplifiableIfStatement
125
-//        if (id == R.id.action_settings) {
126
-//            return true;
127
-//        }
128
-//
129
-//        return super.onOptionsItemSelected(item);
130
-//    }
194
+    @Override
195
+    public boolean onNavigationItemSelected(MenuItem item) {
196
+        if (item.getItemId() == R.id.nav_logout) {
197
+            LoginManager.getInstance().logOut();
198
+            STSBusiness.setLoggedUser(this, null);
199
+            updateLoginState();
200
+        }
201
+        return false;
202
+    }
203
+
204
+    public void updateLoginState()
205
+    {
206
+        UserDbo user = STSBusiness.getLoggedUser(this);
207
+        _btnLogin.setVisibility(user == null ? View.VISIBLE : View.GONE);
208
+        Menu menu = _navigationView.getMenu();
209
+        for (int i = 0; i < menu.size(); i++) {
210
+            menu.getItem(i).setVisible(user != null);
211
+        }
212
+        _textUserName.setText(user == null ? "" : user.getName());
213
+        _textUserName.setVisibility(user == null ? View.GONE : View.VISIBLE);
214
+        _textUserEmail.setText(user == null ? "" : user.getEmail());
215
+        _textUserEmail.setVisibility(user == null ? View.GONE : View.VISIBLE);
216
+    }
217
+
218
+    public void onUserLogged(UserDbo userDbo)
219
+    {
220
+        STSBusiness.setLoggedUser(this, userDbo);
221
+        updateLoginState();
222
+    }
131 223
 }

+ 9
- 0
app/src/main/res/drawable/side_nav_bar.xml View File

@@ -0,0 +1,9 @@
1
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
2
+       android:shape="rectangle">
3
+    <gradient
4
+        android:angle="135"
5
+        android:centerColor="#4CAF50"
6
+        android:endColor="#2E7D32"
7
+        android:startColor="#81C784"
8
+        android:type="linear" />
9
+</shape>

+ 19
- 29
app/src/main/res/layout/activity_main.xml View File

@@ -1,35 +1,25 @@
1 1
 <?xml version="1.0" encoding="utf-8"?>
2
-<android.support.design.widget.CoordinatorLayout
3
-    xmlns:android="http://schemas.android.com/apk/res/android"
4
-    xmlns:app="http://schemas.android.com/apk/res-auto"
5
-    xmlns:tools="http://schemas.android.com/tools"
6
-    android:layout_width="match_parent"
7
-    android:layout_height="match_parent"
8
-    android:fitsSystemWindows="true"
9
-    tools:context=".ui.activities.MainActivity">
2
+<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+                                        xmlns:app="http://schemas.android.com/apk/res-auto"
4
+                                        xmlns:tools="http://schemas.android.com/tools"
5
+                                        android:id="@+id/drawer_layout"
6
+                                        android:layout_width="match_parent"
7
+                                        android:layout_height="match_parent"
8
+                                        android:fitsSystemWindows="true"
9
+                                        tools:openDrawer="start">
10 10
 
11
-    <android.support.design.widget.AppBarLayout
11
+    <include
12
+        layout="@layout/app_bar_main"
12 13
         android:layout_width="match_parent"
13
-        android:layout_height="wrap_content"
14
-        android:theme="@style/AppTheme.AppBarOverlay">
14
+        android:layout_height="match_parent" />
15 15
 
16
-        <android.support.v7.widget.Toolbar
17
-            android:id="@+id/toolbar"
18
-            android:layout_width="match_parent"
19
-            android:layout_height="?attr/actionBarSize"
20
-            android:background="?attr/colorPrimary"
21
-            app:popupTheme="@style/AppTheme.PopupOverlay"/>
22
-
23
-    </android.support.design.widget.AppBarLayout>
24
-
25
-    <include layout="@layout/content_main"/>
26
-
27
-    <android.support.design.widget.FloatingActionButton
28
-        android:id="@+id/fab"
16
+    <android.support.design.widget.NavigationView
17
+        android:id="@+id/nav_view"
29 18
         android:layout_width="wrap_content"
30
-        android:layout_height="wrap_content"
31
-        android:layout_gravity="bottom|end"
32
-        android:layout_margin="@dimen/fab_margin"
33
-        android:src="@drawable/ic_add_black_24dp"/>
19
+        android:layout_height="match_parent"
20
+        android:layout_gravity="start"
21
+        android:fitsSystemWindows="true"
22
+        app:headerLayout="@layout/nav_header_main"
23
+        app:menu="@menu/activity_main_drawer"/>
34 24
 
35
-</android.support.design.widget.CoordinatorLayout>
25
+</android.support.v4.widget.DrawerLayout>

+ 34
- 0
app/src/main/res/layout/app_bar_main.xml View File

@@ -0,0 +1,34 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
4
+                                                 xmlns:tools="http://schemas.android.com/tools"
5
+                                                 android:layout_width="match_parent"
6
+                                                 android:layout_height="match_parent"
7
+                                                 android:fitsSystemWindows="true"
8
+                                                 tools:context=".ui.activities.MainActivity">
9
+
10
+    <android.support.design.widget.AppBarLayout
11
+        android:layout_width="match_parent"
12
+        android:layout_height="wrap_content"
13
+        android:theme="@style/AppTheme.AppBarOverlay">
14
+
15
+        <android.support.v7.widget.Toolbar
16
+            android:id="@+id/toolbar"
17
+            android:layout_width="match_parent"
18
+            android:layout_height="?attr/actionBarSize"
19
+            android:background="?attr/colorPrimary"
20
+            app:popupTheme="@style/AppTheme.PopupOverlay" />
21
+
22
+    </android.support.design.widget.AppBarLayout>
23
+
24
+    <include layout="@layout/content_main" />
25
+
26
+    <android.support.design.widget.FloatingActionButton
27
+        android:id="@+id/fab"
28
+        android:layout_width="wrap_content"
29
+        android:layout_height="wrap_content"
30
+        android:layout_gravity="bottom|end"
31
+        android:layout_margin="@dimen/fab_margin"
32
+        android:src="@drawable/ic_add_black_24dp"/>
33
+
34
+</android.support.design.widget.CoordinatorLayout>

+ 45
- 0
app/src/main/res/layout/nav_header_main.xml View File

@@ -0,0 +1,45 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+              android:layout_width="match_parent"
4
+              android:layout_height="@dimen/nav_header_height"
5
+              android:background="@drawable/side_nav_bar"
6
+              xmlns:tools="http://schemas.android.com/tools"
7
+              android:gravity="bottom"
8
+              android:orientation="vertical"
9
+              android:paddingBottom="@dimen/activity_vertical_margin"
10
+              android:paddingLeft="@dimen/activity_horizontal_margin"
11
+              android:paddingRight="@dimen/activity_horizontal_margin"
12
+              android:paddingTop="@dimen/activity_vertical_margin"
13
+              android:theme="@style/ThemeOverlay.AppCompat.Dark">
14
+
15
+    <ImageView
16
+        android:id="@+id/imageView"
17
+        android:layout_width="wrap_content"
18
+        android:layout_height="wrap_content"
19
+        android:layout_marginTop="@dimen/nav_header_vertical_spacing"
20
+        android:src="@mipmap/ic_launcher" />
21
+
22
+    <TextView
23
+        android:layout_width="match_parent"
24
+        android:layout_height="wrap_content"
25
+        android:layout_marginTop="@dimen/nav_header_vertical_spacing"
26
+        android:id="@+id/textUserName"
27
+        tools:text="Android Studio"
28
+        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
29
+        android:visibility="gone"  />
30
+
31
+    <TextView
32
+        android:id="@+id/textUserEmail"
33
+        android:layout_width="wrap_content"
34
+        android:layout_height="wrap_content"
35
+        tools:text="android.studio@android.com"
36
+        android:visibility="gone" />
37
+
38
+    <com.facebook.login.widget.LoginButton
39
+        android:id="@+id/btnLogin"
40
+        android:layout_width="wrap_content"
41
+        android:layout_height="wrap_content"
42
+        android:layout_marginTop="@dimen/nav_header_vertical_spacing"
43
+        android:layout_gravity="center_horizontal" />
44
+
45
+</LinearLayout>

+ 8
- 0
app/src/main/res/menu/activity_main_drawer.xml View File

@@ -0,0 +1,8 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
3
+
4
+        <item
5
+            android:id="@+id/nav_logout"
6
+            android:icon="@mipmap/ic_menu_logout"
7
+            android:title="@string/logout" />
8
+</menu>

BIN
app/src/main/res/mipmap-hdpi/ic_menu_logout.png View File


BIN
app/src/main/res/mipmap-mdpi/ic_menu_logout.png View File


BIN
app/src/main/res/mipmap-xhdpi/ic_menu_logout.png View File


BIN
app/src/main/res/mipmap-xxhdpi/ic_menu_logout.png View File


BIN
app/src/main/res/mipmap-xxxhdpi/ic_menu_logout.png View File


+ 3
- 0
app/src/main/res/values/dimens.xml View File

@@ -2,5 +2,8 @@
2 2
     <!-- Default screen margins, per the Android Design guidelines. -->
3 3
     <dimen name="activity_horizontal_margin">16dp</dimen>
4 4
     <dimen name="activity_vertical_margin">16dp</dimen>
5
+    <!-- Default screen margins, per the Android Design guidelines. -->
6
+    <dimen name="nav_header_vertical_spacing">16dp</dimen>
7
+    <dimen name="nav_header_height">160dp</dimen>
5 8
     <dimen name="fab_margin">16dp</dimen>
6 9
 </resources>

+ 5
- 0
app/src/main/res/values/strings.xml View File

@@ -2,7 +2,11 @@
2 2
     <!--<string name="banner_ad_unit_id">ca-app-pub-2016581167110949/4561565315</string>-->
3 3
     <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
4 4
     <string name="ad_init">ca-app-pub-2016581167110949~3084832113</string>
5
+    <string name="facebook_app_id">1463583156990249</string>
5 6
 
7
+
8
+    <string name="navigation_drawer_open">Open navigation drawer</string>
9
+    <string name="navigation_drawer_close">Close navigation drawer</string>
6 10
     <string name="app_name">STS Saguenay</string>
7 11
     <string name="title_activity_stop_picker">Pick a stop - STS Saguenay</string>
8 12
     <string name="favourites_stops">Favourites stops:</string>
@@ -16,4 +20,5 @@
16 20
     <string name="favourite_remove">Remove favourite</string>
17 21
     <string name="favourite_remove_confirm">Do you really want to remove %1$s?</string>
18 22
     <string name="favourite_removed">%1$s has been removed</string>
23
+    <string name="logout">Logout</string>
19 24
 </resources>

Loading…
Cancel
Save