Browse Source

Implemented User's Avatar

pull/24/head
Ezerous 8 years ago
parent
commit
64fd18ac44
  1. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java
  2. 45
      app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java
  3. 3
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  4. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  5. 72
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

2
app/src/main/java/gr/thmmy/mthmmy/activities/AboutActivity.java

@ -36,7 +36,7 @@ public class AboutActivity extends BaseActivity
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume();
drawer.setSelection(ABOUT_ID); drawer.setSelection(ABOUT_ID);
super.onResume();
} }
} }

45
app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java

@ -1,14 +1,18 @@
package gr.thmmy.mthmmy.activities; package gr.thmmy.mthmmy.activities;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.View;
import android.widget.ImageView;
import android.widget.Toast; import android.widget.Toast;
import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.PersistentCookieJar;
@ -24,6 +28,9 @@ import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem; import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile; import com.mikepenz.materialdrawer.model.interfaces.IProfile;
import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader;
import com.mikepenz.materialdrawer.util.DrawerImageLoader;
import com.squareup.picasso.Picasso;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.main.MainActivity; import gr.thmmy.mthmmy.activities.main.MainActivity;
@ -65,10 +72,38 @@ public class BaseActivity extends AppCompatActivity
.cookieJar(cookieJar) .cookieJar(cookieJar)
.build(); .build();
sessionManager = new SessionManager(client, cookieJar, sharedPrefsCookiePersistor, sharedPrefs); sessionManager = new SessionManager(client, cookieJar, sharedPrefsCookiePersistor, sharedPrefs);
//initialize and create the image loader logic
DrawerImageLoader.init(new AbstractDrawerImageLoader() {
@Override
public void set(ImageView imageView, Uri uri, Drawable placeholder) {
Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder).into(imageView);
}
@Override
public void cancel(ImageView imageView) {
Picasso.with(imageView.getContext()).cancelRequest(imageView);
}
@Override
public Drawable placeholder(Context ctx, String tag) {
if (DrawerImageLoader.Tags.PROFILE.name().equals(tag)) {
return new IconicsDrawable(ctx, FontAwesome.Icon.faw_user_circle);
}
return super.placeholder(ctx, tag);
}
});
init = true; init = true;
} }
} }
@Override
protected void onResume() {
super.onResume();
updateDrawer();
}
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
@ -123,7 +158,7 @@ public class BaseActivity extends AppCompatActivity
aboutItem = new PrimaryDrawerItem().withIdentifier(ABOUT_ID).withName(R.string.about).withIcon(aboutIcon); aboutItem = new PrimaryDrawerItem().withIdentifier(ABOUT_ID).withName(R.string.about).withIcon(aboutIcon);
//Profile //Profile
profileDrawerItem = new ProfileDrawerItem().withName(sessionManager.getUsername()); //TODO: set profile picture profileDrawerItem = new ProfileDrawerItem().withName(sessionManager.getUsername());
//AccountHeader //AccountHeader
accountHeader = new AccountHeaderBuilder() accountHeader = new AccountHeaderBuilder()
@ -201,11 +236,15 @@ public class BaseActivity extends AppCompatActivity
if(drawer!=null) if(drawer!=null)
{ {
if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest
{
loginLogoutItem.withName(R.string.login).withIcon(loginIcon); //Swap logout with login loginLogoutItem.withName(R.string.login).withIcon(loginIcon); //Swap logout with login
profileDrawerItem.withName(sessionManager.getUsername()).withIcon(FontAwesome.Icon.faw_user_circle);
}
else else
{
loginLogoutItem.withName(R.string.logout).withIcon(logoutIcon); //Swap login with logout loginLogoutItem.withName(R.string.logout).withIcon(logoutIcon); //Swap login with logout
profileDrawerItem.withName(sessionManager.getUsername()).withIcon(sessionManager.getAvatarLink());
profileDrawerItem.withName(sessionManager.getUsername()); //TODO: set profile picture }
accountHeader.updateProfile(profileDrawerItem); accountHeader.updateProfile(profileDrawerItem);
drawer.updateItem(loginLogoutItem); drawer.updateItem(loginLogoutItem);

3
app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java

@ -60,9 +60,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume();
drawer.setSelection(HOME_ID); drawer.setSelection(HOME_ID);
updateDrawer(); super.onResume();
} }
@Override @Override

2
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -180,8 +180,8 @@ public class TopicActivity extends BaseActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume();
drawer.setSelection(-1); drawer.setSelection(-1);
super.onResume();
} }
@Override @Override

72
app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

@ -1,7 +1,6 @@
package gr.thmmy.mthmmy.session; package gr.thmmy.mthmmy.session;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Environment;
import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
@ -11,7 +10,6 @@ import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InterruptedIOException; import java.io.InterruptedIOException;
import java.util.List; import java.util.List;
@ -63,6 +61,7 @@ public class SessionManager
//Shared Preferences & its keys //Shared Preferences & its keys
private SharedPreferences sharedPrefs; private SharedPreferences sharedPrefs;
public static final String USERNAME = "Username"; public static final String USERNAME = "Username";
public static final String AVATAR_LINK = "AvatarUrl";
public static final String LOGOUT_LINK = "LogoutLink"; public static final String LOGOUT_LINK = "LogoutLink";
public static final String LOGIN_STATUS = "IsLoggedIn"; public static final String LOGIN_STATUS = "IsLoggedIn";
@ -125,6 +124,7 @@ public class SessionManager
//Edit SharedPreferences, save session's data //Edit SharedPreferences, save session's data
sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_IN).apply(); sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_IN).apply();
sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply();
sharedPrefs.edit().putString(AVATAR_LINK, extractAvatarLink(document)).apply();
sharedPrefs.edit().putString(LOGOUT_LINK, HttpUrl.parse(logoutButton.attr("href")).toString()).apply(); sharedPrefs.edit().putString(LOGOUT_LINK, HttpUrl.parse(logoutButton.attr("href")).toString()).apply();
return SUCCESS; return SUCCESS;
@ -248,6 +248,10 @@ public class SessionManager
return sharedPrefs.getString(USERNAME, "Username"); return sharedPrefs.getString(USERNAME, "Username");
} }
public String getAvatarLink() {
return sharedPrefs.getString(AVATAR_LINK, "AvatarUrl");
}
public int getLogStatus() { public int getLogStatus() {
return sharedPrefs.getInt(LOGIN_STATUS, LOGGED_OUT); return sharedPrefs.getInt(LOGIN_STATUS, LOGGED_OUT);
} }
@ -299,70 +303,22 @@ public class SessionManager
return matcher.group(1); return matcher.group(1);
} }
} }
Report.w(TAG,"Extracting username failed!");
return null; return null;
} }
/*private File getUserAvatar(String image_url, final String package_name, final String image_name) { private String extractAvatarLink(Document doc)
final File[] returnImage = {null}; {
if (doc != null) {
ImageController.getInstance().getImageLoader().get(image_url, new ImageLoader.ImageListener() { Elements avatar = doc.select("#ava img");
@Override
public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
Bitmap bitmap = imageContainer.getBitmap(); //The image
//Create a file to store it
File pictureFile = getOutputMediaFile(package_name, image_name);
if (pictureFile == null) { if (avatar.size() == 1) {
Report.d(TAG, return avatar.attr("src");
"Error creating media file, check storage permissions: ");
return;
}
try {
//Initialize an output stream
FileOutputStream fos = new FileOutputStream(pictureFile);
//Store image to file
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
Report.d(TAG, "File not found");
} catch (IOException e) {
Report.d(TAG, "Error accessing file"));
} }
returnImage[0] = pictureFile;
} }
Report.w(TAG,"Extracting avatar's link failed!");
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
return returnImage[0];
}*/
/** Create a File for saving an image or video */
private File getOutputMediaFile(String packageName, String imageName){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ packageName
+ "/Files");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null; return null;
} }
}
// Create a media file name
File mediaFile;
String mImageName = imageName + ".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
//----------------------------------OTHER FUNCTIONS END----------------------------------------- //----------------------------------OTHER FUNCTIONS END-----------------------------------------
} }
Loading…
Cancel
Save