Bladeren bron

测试模式下添加切换账号功能

zengjiebin 7 jaren geleden
bovenliggende
commit
9cdea7a536

+ 1 - 0
app/src/main/java/com/kfzs/duanduan/fragment/FgtSmallSheep.java

@@ -214,6 +214,7 @@ public class FgtSmallSheep extends BaseCompatFragment implements SmallSheepContr
         initView();
         initListener();
         TestUtil.testChange(getActivity(), (TextView) getViewById(R.id.test_change));
+        TestUtil.testChangeUser(getActivity(), (TextView) getViewById(R.id.test_change_user));
     }
 
 

+ 64 - 0
app/src/main/java/com/sheep/gamegroup/model/entity/LoginUser.java

@@ -0,0 +1,64 @@
+package com.sheep.gamegroup.model.entity;
+
+import org.xutils.db.annotation.Column;
+import org.xutils.db.annotation.Table;
+
+/**
+ * Created by realicing on 2018/6/13.
+ * realicing@sina.com
+ */
+@Table(name = "login_user")
+public class LoginUser {
+    @Column(name = "id", isId = true)
+    private String id;
+    @Column(name = "token")
+    private String token;
+    @Column(name = "nickname")
+    private String nickname;
+    @Column(name = "service_name")
+    private String serviceName;
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getToken() {
+        return token;
+    }
+
+    public void setToken(String token) {
+        this.token = token;
+    }
+
+    public String getNickname() {
+        return nickname;
+    }
+
+    public void setNickname(String nickname) {
+        this.nickname = nickname;
+    }
+
+    public String getServiceName() {
+        return serviceName;
+    }
+
+    public void setServiceName(String serviceName) {
+        this.serviceName = serviceName;
+    }
+
+
+
+
+
+
+
+
+    @Override
+    public String toString() {
+        return nickname;
+    }
+}

+ 2 - 4
app/src/main/java/com/sheep/gamegroup/presenter/LoginPresenter.java

@@ -1,9 +1,6 @@
 package com.sheep.gamegroup.presenter;
 
-import android.text.TextUtils;
-
 import com.alibaba.fastjson.JSONObject;
-
 import com.sheep.gamegroup.model.api.ApiService;
 import com.sheep.gamegroup.model.entity.BaseMessage;
 import com.sheep.gamegroup.model.entity.LoginEntity;
@@ -13,11 +10,11 @@ import com.sheep.gamegroup.util.ChannelContent;
 import com.sheep.gamegroup.util.FastJsonUtils;
 import com.sheep.gamegroup.util.MyDbManager;
 import com.sheep.gamegroup.util.PreferenceUtils;
+import com.sheep.gamegroup.util.TestUtil;
 import com.sheep.gamegroup.util.UMConfigUtils;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.SpUtils;
 
-
 import org.xutils.ex.DbException;
 
 import javax.inject.Inject;
@@ -96,5 +93,6 @@ public class LoginPresenter implements LoginContract.Presenter {
             }
         }
         view.NetSuccess(1,"登录成功", EntityUtils.getUserCode(loginEty));
+        TestUtil.saveUser(loginEty);
     }
 }

+ 10 - 7
app/src/main/java/com/sheep/gamegroup/util/LogUtil.java

@@ -8,12 +8,15 @@ import com.sheep.jiuyan.samllsheep.SheepApp;
  * realicing@sina.com
  */
 public class LogUtil {
-    public static void println(String msg) {
-        if(BuildConfig.DEBUG || SheepApp.getInstance().connectAddress != ConnectAddress.sheep)
-            System.out.println(msg);
-    }
-    public static void println(String tag, String msg) {
-        if(BuildConfig.DEBUG || SheepApp.getInstance().connectAddress != ConnectAddress.sheep)
-            System.out.println(tag+" "+msg);
+    public static void println(Object... msgs) {
+        if(BuildConfig.DEBUG || SheepApp.getInstance().connectAddress != ConnectAddress.sheep) {
+            for (int i = 0; i < msgs.length; i++) {
+                if(i != 0) {
+                    System.out.print(" ");
+                }
+                System.out.print(msgs[i]);
+            }
+            System.out.println();
+        }
     }
 }

+ 12 - 1
app/src/main/java/com/sheep/gamegroup/util/MyDbManager.java

@@ -4,8 +4,8 @@ import android.database.Cursor;
 import android.os.Handler;
 
 import com.sheep.gamegroup.model.entity.AppRecord;
+import com.sheep.gamegroup.model.entity.LoginUser;
 import com.sheep.gamegroup.model.entity.NewbieTaskRecord;
-import com.sheep.gamegroup.model.entity.TaskEty;
 import com.sheep.gamegroup.model.entity.TaskListen;
 import com.sheep.gamegroup.model.entity.UserEntity;
 import com.sheep.jiuyan.samllsheep.SheepApp;
@@ -186,4 +186,15 @@ public class MyDbManager {
             e.printStackTrace();
         }
     }
+
+    public List<LoginUser> getAllLoginUser() {
+        try{
+            return db.selector(LoginUser.class)
+                    .where("service_name", "=", SheepApp.getInstance().getConnectAddress().getName())
+                    .findAll();
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+        return ListUtil.emptyList();
+    }
 }

+ 69 - 4
app/src/main/java/com/sheep/gamegroup/util/TestUtil.java

@@ -14,24 +14,24 @@ import android.widget.TextView;
 
 import com.alibaba.fastjson.JSON;
 import com.sheep.gamegroup.model.entity.BaseMessage;
-import com.sheep.gamegroup.model.entity.CheckUserLabel;
 import com.sheep.gamegroup.model.entity.CreditCard;
 import com.sheep.gamegroup.model.entity.DialogConfig;
+import com.sheep.gamegroup.model.entity.LoginEntity;
+import com.sheep.gamegroup.model.entity.LoginUser;
 import com.sheep.gamegroup.model.entity.TaskEty;
-import com.sheep.gamegroup.model.entity.UserLabel;
-import com.sheep.gamegroup.model.entity.UserLabelList;
+import com.sheep.gamegroup.model.entity.UserEntity;
 import com.sheep.gamegroup.model.util.SheepSubscriber;
 import com.sheep.jiuyan.samllsheep.BuildConfig;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.G;
+import com.sheep.jiuyan.samllsheep.utils.SpUtils;
 import com.umeng.socialize.ShareAction;
 import com.umeng.socialize.UMShareListener;
 import com.umeng.socialize.bean.SHARE_MEDIA;
 import com.umeng.socialize.media.UMImage;
 
 import java.io.File;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
 
@@ -173,6 +173,71 @@ public class TestUtil {
             });
         }
     }
+    public static void testChangeUser(final Activity activity, TextView test_change) {
+        if (isTest()) {
+            final List<LoginUser> loginUserList = MyDbManager.getInstance().getAllLoginUser();
+            if(ListUtil.isEmpty(loginUserList)){
+                G.showToast("无用户");
+                return;
+            }
+            final List<Object> list = ListUtil.emptyList();
+            ListUtil.addAll(list, loginUserList);
+
+            test_change.setVisibility(View.VISIBLE);
+            test_change.setText("切换账号");
+            test_change.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View view) {
+                    AlertDialog dialog = new AlertDialog.Builder(activity).setTitle("选择要登录的账号")
+                            .setAdapter(new ArrayAdapter<>(activity, android.R.layout.simple_list_item_single_choice, list),  new DialogInterface.OnClickListener() {
+
+                                @Override
+                                public void onClick(DialogInterface dialogInterface, int which) {
+                                    Object object = ListUtil.getIndex(list, which);
+                                    if(object instanceof LoginUser){
+                                        SpUtils.login(activity, ((LoginUser) object).getToken());
+                                        Jump2View.getInstance().goHomePageView(activity, null);
+                                        activity.finish();
+                                    }
+                                }
+                            }).create();
+                    dialog.show();
+                }
+            });
+        }
+    }
+    public static void saveUser(LoginEntity loginEty) {
+        if(loginEty != null && loginEty.getUser() != null) {
+            LoginUser loginUser = new LoginUser();
+            loginUser.setId(loginEty.getUser().getId());
+            loginUser.setToken(loginEty.getToken());
+            loginUser.setServiceName(SheepApp.getInstance().getConnectAddress().getName());
+            loginUser.setNickname(loginEty.getUser().getNickname());
+            MyDbManager.getInstance().saveOrUpdate(loginUser);
+        }
+    }
+    public static void addUser(final String token) {
+        SheepApp.getInstance().getNetComponent().getApiService().getInfo()
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
+                    @Override
+                    public void onNext(BaseMessage baseMessage) {
+                        LogUtil.println("baseMessage onNext "+ JSON.toJSONString(baseMessage));
+                        UserEntity userEntity = baseMessage.getData(UserEntity.class);
+                        LoginEntity loginEntity = new LoginEntity();
+                        loginEntity.setToken(token);
+                        loginEntity.setUser(userEntity);
+                        saveUser(loginEntity);
+                    }
+
+                    @Override
+                    public void onError(BaseMessage baseMessage) {
+                        LogUtil.println("baseMessage onError "+JSON.toJSONString(baseMessage));
+                        G.showToast(baseMessage);
+                    }
+                });
+    }
     public static boolean isTest(){
         return BuildConfig.DEBUG || !"sheep".equals(BuildConfig.FLAVOR);
     }

+ 1 - 0
app/src/main/java/com/sheep/gamegroup/view/activity/LoginAct.java

@@ -57,6 +57,7 @@ public class LoginAct extends BaseUMActivity implements LoginContract.View {
                 .build()
                 .inject(this);
         TestUtil.testChange(this, (TextView) findViewById(R.id.test_change));
+        TestUtil.testChangeUser(this, (TextView) findViewById(R.id.test_change_user));
 
     }
 

+ 3 - 6
app/src/main/java/com/sheep/gamegroup/view/activity/PhoneAct.java

@@ -13,9 +13,7 @@ import android.view.inputmethod.InputMethodManager;
 import android.widget.ImageView;
 import android.widget.TextView;
 
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-
 import com.sheep.gamegroup.absBase.BaseActivity;
 import com.sheep.gamegroup.di.components.DaggerPhoneComponent;
 import com.sheep.gamegroup.di.modules.PhoneModule;
@@ -29,17 +27,13 @@ import com.sheep.gamegroup.util.ActivityManager;
 import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.util.MyDbManager;
 import com.sheep.gamegroup.util.SelfCountDownTimer;
-
 import com.sheep.gamegroup.util.TestUtil;
-import com.sheep.jiuyan.samllsheep.BuildConfig;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.G;
 import com.sheep.jiuyan.samllsheep.utils.SpUtils;
 import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
 
-import org.xutils.ex.DbException;
-
 import javax.inject.Inject;
 
 import butterknife.BindView;
@@ -158,6 +152,8 @@ public class PhoneAct extends BaseActivity implements PhoneContract.View {
                     if(TextUtils.isEmpty(etPhone))
                         etPhone = "13626@395a75211751465f54789a7f459089bd";
                     goMain(etPhone);
+                    if(!TextUtils.isEmpty(etPhone))
+                        TestUtil.addUser(etPhone);
                     return;
                 }
                 loginByPhoneGaptcha();
@@ -266,6 +262,7 @@ public class PhoneAct extends BaseActivity implements PhoneContract.View {
 
         Jump2View.getInstance().goHomePageView(this, EntityUtils.getUserCode(loginEty));
         Log.e("---login_-", o.toString());
+        TestUtil.saveUser(loginEty);
     }
 
     @Override

+ 9 - 1
app/src/main/java/com/sheep/jiuyan/samllsheep/service/MonitorAppService.java

@@ -84,6 +84,14 @@ public class MonitorAppService extends AccessibilityService {
         return super.onUnbind(intent);
     }
 
+    public static void closeAccessibility() {
+        String service = SheepApp.getInstance().getPackageName() + "/" + MonitorAppService.class.getCanonicalName();
+        // 这个key保存已经辅助服务的组件名列表,以“:”隔开,要关闭自己的组件,只要从这个key的值中移除该字符串
+        Settings.Secure.putString(SheepApp.getInstance().getApplicationContext().getContentResolver(),Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, service);//com.sheep.jiuyan.samllsheep/com.sheep.jiuyan.samllsheep.service.MonitorAppService
+        // 这个key控制辅助服务的开关
+        Settings.Secure.putInt(SheepApp.getInstance().getApplicationContext().getContentResolver(),Settings.Secure.ACCESSIBILITY_ENABLED, 0);
+    }
+
     public static boolean isAccessibilitySettingsOn(Context mContext) {
         int accessibilityEnabled = 0;
         final String service = mContext.getPackageName() + "/" + MonitorAppService.class.getCanonicalName();
@@ -98,7 +106,7 @@ public class MonitorAppService extends AccessibilityService {
         if (accessibilityEnabled == 1) {
             String settingValue = Settings.Secure.getString(mContext.getApplicationContext().getContentResolver(),
                     Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
-            // com.z.buildingaccessibilityservices/com.z.buildingaccessibilityservices.TestService
+            //com.sheep.jiuyan.samllsheep/com.sheep.jiuyan.samllsheep.service.MonitorAppService
             if (settingValue != null) {
                 mStringColonSplitter.setString(settingValue);
                 while (mStringColonSplitter.hasNext()) {

+ 12 - 0
app/src/main/res/layout/homepage_act_layout.xml

@@ -97,4 +97,16 @@
         android:layout_alignParentStart="true"
         android:text="切换服务器"
         android:visibility="gone"/>
+    <TextView
+        android:id="@+id/test_change_user"
+        style="@style/style_button_small_yellow"
+        android:layout_width="wrap_content"
+        android:layout_height="30dp"
+        android:paddingStart="@dimen/content_padding_10"
+        android:paddingEnd="@dimen/content_padding_10"
+        android:layout_margin="@dimen/content_padding_10"
+        android:layout_above="@+id/test_change"
+        android:layout_alignParentStart="true"
+        android:text="切换账号"
+        android:visibility="gone"/>
 </RelativeLayout>

+ 12 - 0
app/src/main/res/layout/login_act_layout.xml

@@ -89,4 +89,16 @@
         android:layout_alignParentEnd="true"
         android:text="切换服务器"
         android:visibility="gone"/>
+    <TextView
+        android:id="@+id/test_change_user"
+        style="@style/style_button_small_yellow"
+        android:layout_width="wrap_content"
+        android:layout_height="30dp"
+        android:paddingStart="@dimen/content_padding_10"
+        android:paddingEnd="@dimen/content_padding_10"
+        android:layout_margin="@dimen/content_padding_10"
+        android:layout_below="@+id/test_change"
+        android:layout_alignParentEnd="true"
+        android:text="切换账号"
+        android:visibility="gone"/>
 </RelativeLayout>