|
|
@ -1,7 +1,6 @@ |
|
|
|
package gr.thmmy.mthmmy.session; |
|
|
|
|
|
|
|
import android.content.SharedPreferences; |
|
|
|
import android.os.Environment; |
|
|
|
|
|
|
|
import com.franmontiel.persistentcookiejar.PersistentCookieJar; |
|
|
|
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; |
|
|
@ -11,7 +10,6 @@ import org.jsoup.nodes.Document; |
|
|
|
import org.jsoup.nodes.Element; |
|
|
|
import org.jsoup.select.Elements; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InterruptedIOException; |
|
|
|
import java.util.List; |
|
|
@ -63,6 +61,7 @@ public class SessionManager |
|
|
|
//Shared Preferences & its keys
|
|
|
|
private SharedPreferences sharedPrefs; |
|
|
|
public static final String USERNAME = "Username"; |
|
|
|
public static final String AVATAR_LINK = "AvatarUrl"; |
|
|
|
public static final String LOGOUT_LINK = "LogoutLink"; |
|
|
|
public static final String LOGIN_STATUS = "IsLoggedIn"; |
|
|
|
|
|
|
@ -125,6 +124,7 @@ public class SessionManager |
|
|
|
//Edit SharedPreferences, save session's data
|
|
|
|
sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_IN).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(); |
|
|
|
|
|
|
|
return SUCCESS; |
|
|
@ -248,6 +248,10 @@ public class SessionManager |
|
|
|
return sharedPrefs.getString(USERNAME, "Username"); |
|
|
|
} |
|
|
|
|
|
|
|
public String getAvatarLink() { |
|
|
|
return sharedPrefs.getString(AVATAR_LINK, "AvatarUrl"); |
|
|
|
} |
|
|
|
|
|
|
|
public int getLogStatus() { |
|
|
|
return sharedPrefs.getInt(LOGIN_STATUS, LOGGED_OUT); |
|
|
|
} |
|
|
@ -299,70 +303,22 @@ public class SessionManager |
|
|
|
return matcher.group(1); |
|
|
|
} |
|
|
|
} |
|
|
|
Report.w(TAG,"Extracting username failed!"); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/*private File getUserAvatar(String image_url, final String package_name, final String image_name) { |
|
|
|
final File[] returnImage = {null}; |
|
|
|
|
|
|
|
ImageController.getInstance().getImageLoader().get(image_url, new ImageLoader.ImageListener() { |
|
|
|
@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) { |
|
|
|
Report.d(TAG, |
|
|
|
"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; |
|
|
|
} |
|
|
|
private String extractAvatarLink(Document doc) |
|
|
|
{ |
|
|
|
if (doc != null) { |
|
|
|
Elements avatar = doc.select("#ava img"); |
|
|
|
|
|
|
|
@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; |
|
|
|
if (avatar.size() == 1) { |
|
|
|
return avatar.attr("src"); |
|
|
|
} |
|
|
|
} |
|
|
|
// Create a media file name
|
|
|
|
File mediaFile; |
|
|
|
String mImageName = imageName + ".jpg"; |
|
|
|
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName); |
|
|
|
return mediaFile; |
|
|
|
Report.w(TAG,"Extracting avatar's link failed!"); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------------------OTHER FUNCTIONS END-----------------------------------------
|
|
|
|
|
|
|
|
} |