Skip to content

Commit

Permalink
fix login problems
Browse files Browse the repository at this point in the history
  • Loading branch information
kidozh committed May 31, 2020
1 parent 0e937e1 commit f5c1b4f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand Down Expand Up @@ -372,12 +373,13 @@ void sendLoginRequest(){

FormBody.Builder formBodyBuilder = new FormBody.Builder()
.add("loginfield", "username")
//.add("cookietime", "2592000")
.add("cookietime", "2592000")
.add("username", account)
.add("password", password)
.add("questionid",String.valueOf(bbsSecurityQuestionSpinner.getSelectedItemPosition()))
.add("answer",bbsSecurityAnswerEditText.getText().toString())

.add("quickforward", "yes")
.add("handlekey", "1s")
.add("referer",curBBS.base_url);

if(needCaptcha()){
Expand Down Expand Up @@ -416,7 +418,8 @@ public void run() {

@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.body()!=null){
if(response.isSuccessful() && response.body()!=null){

String res = response.body().string();
// fetch the api URL
Log.d(TAG,"get result json "+res);
Expand All @@ -430,9 +433,10 @@ public void onResponse(Call call, Response response) throws IOException {
}
if(loginResult !=null && loginResult.variables !=null){
forumUserBriefInfo parsedUserInfo = loginResult.variables.getUserBriefInfo();
if(parsedUserInfo !=null && ! parsedUserInfo.uid.equals("0")){
if(parsedUserInfo !=null
&& loginResult.message!=null
&& loginResult.message.key.equals("login_succeed")){
// successful

parsedUserInfo.belongedBBSID = curBBS.getId();
if(curUser !=null){
// relogin user
Expand All @@ -442,8 +446,13 @@ public void onResponse(Call call, Response response) throws IOException {
}
else {
if(loginResult.message!=null){
if(loginResult.message.key.equals("login_seccheck2")){
Log.d(TAG,"Need seccode to login ");
viewModel.getSecureInfo();
}
viewModel.error.postValue(true);
viewModel.errorString.postValue(loginResult.message.content);

}
else {
viewModel.error.postValue(true);
Expand Down Expand Up @@ -494,10 +503,20 @@ protected void onPostExecute(Void aVoid) {
Log.d(TAG,"save user to database id: "+userBriefInfo.getId()+" "+insertedId);
userBriefInfo.setId((int) insertedId);
// transiting data
networkUtils.copySharedPrefence(
context.getSharedPreferences("CookiePersistence",Context.MODE_PRIVATE),
context.getSharedPreferences(networkUtils.getSharedPreferenceNameByUser(userBriefInfo),Context.MODE_PRIVATE)
);
if(curUser !=null){
// relogin user
networkUtils.copySharedPrefence(
context.getSharedPreferences(networkUtils.getSharedPreferenceNameByUser(curUser),Context.MODE_PRIVATE),
context.getSharedPreferences(networkUtils.getSharedPreferenceNameByUser(userBriefInfo),Context.MODE_PRIVATE)
);
}
else {
networkUtils.copySharedPrefence(
context.getSharedPreferences("CookiePersistence",Context.MODE_PRIVATE),
context.getSharedPreferences(networkUtils.getSharedPreferenceNameByUser(userBriefInfo),Context.MODE_PRIVATE)
);
}

Log.d(TAG, "Transiting data to preference "+networkUtils.getSharedPreferenceNameByUser(userBriefInfo));

finishAfterTransition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private void getIntentInfo(){

private void getNotificationInfo(){
Request request = new Request.Builder()
.url(URLUtils.getLoginApiUrl())
.url(URLUtils.getLoginSecondaryUrl())
.build();
Handler mHandler = new Handler(Looper.getMainLooper());
client.newCall(request).enqueue(new Callback() {
Expand Down Expand Up @@ -337,7 +337,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(this, UserProfileActivity.class);
intent.putExtra(bbsConstUtils.PASS_BBS_ENTITY_KEY,curBBS);
intent.putExtra(bbsConstUtils.PASS_BBS_USER_KEY,curUser);
intent.putExtra("UID",String.valueOf(curUser.uid));
intent.putExtra("UID",curUser.uid);
startActivity(intent);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void authUserIntergrity(){
client.cookieJar().saveFromResponse(httpUrl,cookieList);
// exact login url

String loginApiUrl = URLUtils.getLoginApiUrl();
String loginApiUrl = URLUtils.getLoginSecondaryUrl();
Request request = new Request.Builder()
.url(loginApiUrl)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
Expand Down Expand Up @@ -55,6 +59,8 @@

import org.xml.sax.XMLReader;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -434,24 +440,59 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) {

}

public Bitmap DrawableToBitmap(Drawable drawable) {

// 获取 drawable 长宽
int width = drawable.getIntrinsicWidth();
int heigh = drawable.getIntrinsicHeight();

drawable.setBounds(0, 0, width, heigh);

// 获取drawable的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 创建bitmap
Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
// 创建bitmap画布
Canvas canvas = new Canvas(bitmap);
// 将drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}

@Override
public void onResourceReady(final Drawable resource, Transition<? super Drawable> transition) {
final Drawable drawable = resource;

//获取原图大小
textView.post(new Runnable() {
@Override
public void run() {
Drawable drawable = resource;
int width=drawable.getIntrinsicWidth() ;
int height=drawable.getIntrinsicHeight();
final int DRAWABLE_COMPRESS_THRESHOLD = 250000;
final int DRAWABLE_SIMLEY_THRESHOLD = 10000;
// Rescale to image
// int screenWidth = outMetrics.widthPixels - textView.getPaddingLeft() - textView.getPaddingRight();
//int screenWidth = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
int screenWidth = textView.getMeasuredWidth();
Log.d(TAG,"Screen width "+screenWidth+" image width "+width);
if (screenWidth / width < 4 && screenWidth !=0 ){


if (screenWidth !=0 && width * height > DRAWABLE_SIMLEY_THRESHOLD){
double rescaleFactor = ((double) screenWidth) / width;
int newHeight = (int) (height * rescaleFactor);
Log.d(TAG,"rescaleFactor "+rescaleFactor+" image new height "+newHeight);
if(width * height > DRAWABLE_COMPRESS_THRESHOLD){
// compress it for swift display
Bitmap bitmap = DrawableToBitmap(drawable);
// scale it first
bitmap = Bitmap.createScaledBitmap(bitmap,screenWidth, newHeight, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,80, out);
Bitmap compressedBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
drawable = new BitmapDrawable(context.getResources(), compressedBitmap);

}

myDrawable.setBounds(0,0,screenWidth,newHeight);
drawable.setBounds(0,0,screenWidth,newHeight);
resource.setBounds(0,0,screenWidth,newHeight);
Expand All @@ -469,13 +510,10 @@ else if(screenWidth == 0){
resource.setBounds(0,0,width*2,height*2);
}

//myDrawable.invalidateSelf();
myDrawable.setDrawable(drawable);
TextView tv = textView;
tv.setText(tv.getText());
tv.invalidate();
//tv.setText(tv.getText(), TextView.BufferType.SPANNABLE);
//tv.invalidate();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private Result fetchAndParseInfo(){
Set<String> notificationSituation = prefs.getStringSet(context.getString(R.string.preference_key_recv_notification_situation),new HashSet<>());

Request request = new Request.Builder()
.url(URLUtils.getLoginApiUrl())
.url(URLUtils.getLoginSecondaryUrl())
.build();

try{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static String getLoginUrl(){
.appendQueryParameter("module","login")
.appendQueryParameter("action","login")
.appendQueryParameter("loginsubmit","yes")
//.appendQueryParameter("inajax","1")
.appendQueryParameter("inajax","1")
.build();
return uri.toString();
}
Expand All @@ -247,7 +247,7 @@ public static String getLoginWebUrl(){
return BASE_URL+"/member.php?mod=logging&action=login";
}

public static String getLoginApiUrl(){
public static String getLoginSecondaryUrl(){
return BASE_URL + "/api/mobile/index.php?version=4&module=login&mod=logging&action=login";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ public static OkHttpClient getSafeOkHttpClient(Context context){
}

public static OkHttpClient getSafeOkHttpClientWithCookieJar(Context context){
SharedPreferences sharedPreferences = context.getSharedPreferences("CookiePersistence", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();

ClearableCookieJar cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));
cookieJar.clearSession();
Expand Down Expand Up @@ -146,6 +143,7 @@ public static void copySharedPrefence(SharedPreferences fromSp, SharedPreference
for(Map.Entry<String,?> entry : sp.getAll().entrySet()){
Object v = entry.getValue();
String key = entry.getKey();
Log.d(TAG,"Transition "+key+" val :"+v.toString());
//Now we just figure out what type it is, so we can copy it.
// Note that i am using Boolean and Integer instead of boolean and int.
// That's because the Entry class can only hold objects and int and boolean are primatives.
Expand All @@ -162,7 +160,7 @@ else if(v instanceof Long)
else if(v instanceof String)
ed.putString(key, ((String)v));
}
ed.commit(); //save it.
ed.apply(); //save it.
}

public static String getSharedPreferenceNameByUser(@NonNull forumUserBriefInfo briefInfo){
Expand Down Expand Up @@ -375,10 +373,6 @@ public static OkHttpClient getUnsafeOkHttpClient(Context context){
}

public static OkHttpClient getUnsafeOkHttpClientWithCookieJar(Context context){
SharedPreferences sharedPreferences = context.getSharedPreferences("CookiePersistence", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
ClearableCookieJar cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));
cookieJar.clearSession();
Expand Down

0 comments on commit f5c1b4f

Please sign in to comment.