Procházet zdrojové kódy

可以轻松切换服务器

zengjiebin před 8 roky
rodič
revize
d329ebf9ae

+ 0 - 3
app/src/main/java/com/kfzs/duanduan/ActSearch.java

@@ -54,11 +54,9 @@ import com.kfzs.duanduan.adp.AdpSearchGame;
 import com.kfzs.duanduan.adp.AdpSearchRecord;
 import com.kfzs.duanduan.adp.AdpSearchViewPager;
 import com.orhanobut.logger.Logger;
-import com.sheep.gamegroup.util.ConnectAddress;
 import com.sheep.gamegroup.util.UMConfigUtils;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
-import com.sheep.jiuyan.samllsheep.utils.G;
 import com.umeng.analytics.MobclickAgent;
 
 import org.greenrobot.eventbus.EventBus;
@@ -326,7 +324,6 @@ public class ActSearch extends BaseCompatActivity {
                     showRecord(true);
                     return;
                 }
-                SheepApp.getInstance().setConnectAddress(ActSearch.this, mEdtKeyWord.getText().toString());
             }
         });
 

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

@@ -2,12 +2,14 @@ package com.kfzs.duanduan.fragment;
 
 import android.app.Activity;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
 import android.support.v4.view.ViewPager;
 import android.support.v4.widget.SwipeRefreshLayout;
+import android.support.v7.app.AlertDialog;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
@@ -63,6 +65,7 @@ import com.sheep.gamegroup.presenter.TryMakeMoneyContract;
 import com.sheep.gamegroup.presenter.TryMakeMoneyPresenter;
 import com.sheep.gamegroup.util.AppUtil;
 import com.sheep.gamegroup.util.CommonUtil;
+import com.sheep.gamegroup.util.ConnectAddress;
 import com.sheep.gamegroup.util.ImageGlarryDrawable;
 import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.util.MyDbManager;
@@ -82,6 +85,7 @@ import com.umeng.analytics.MobclickAgent;
 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
 import org.greenrobot.eventbus.ThreadMode;
+import org.w3c.dom.Text;
 import org.xutils.ex.DbException;
 
 import java.text.SimpleDateFormat;
@@ -152,6 +156,9 @@ public class FgtSmallSheep extends BaseCompatFragment implements SmallSheepContr
     @BindView(R.id.recyclerview)
     RecyclerView recyclerview;
 
+    @BindView(R.id.test_change)
+    TextView test_change;
+
     @Inject
     SmallSheepPresenter pagePresenter;
     @Inject
@@ -190,6 +197,7 @@ public class FgtSmallSheep extends BaseCompatFragment implements SmallSheepContr
         setContentView(R.layout.homepage_act_layout);
         initView();
         initListener();
+        testChange();
     }
 
     public void initView() {
@@ -901,4 +909,36 @@ public class FgtSmallSheep extends BaseCompatFragment implements SmallSheepContr
 
 
     }
+    private void testChange(){
+        if(!"sheep".equals(BuildConfig.FLAVOR)){
+            test_change.setVisibility(View.VISIBLE);
+            test_change.setText(SheepApp.getInstance().getConnectAddress().getName());
+            test_change.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View view) {
+
+                    final String[] items = {"张伟", "测试服", "正式服"};
+                    AlertDialog dialog = new AlertDialog.Builder(activity).setTitle("请选择要切换的服务器")
+                            .setItems(items, new DialogInterface.OnClickListener() {
+
+                                @Override
+                                public void onClick(DialogInterface dialog, int which) {
+                                    switch (items[which]){
+                                        case "张伟":
+                                            SheepApp.getInstance().setConnectAddress(ConnectAddress.sheepzhangwei);
+                                            break;
+                                        case "测试服":
+                                            SheepApp.getInstance().setConnectAddress(ConnectAddress.sheeptest);
+                                            break;
+                                        case "正式服":
+                                            SheepApp.getInstance().setConnectAddress(ConnectAddress.sheep);
+                                            break;
+                                    }
+                                }
+                            }).create();
+                    dialog.show();
+                }
+            });
+        }
+    }
 }

+ 61 - 0
app/src/main/java/com/kfzs/duanduan/view/DragRelativeLayout.java

@@ -0,0 +1,61 @@
+package com.kfzs.duanduan.view;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.widget.RelativeLayout;
+
+import com.sheep.jiuyan.samllsheep.R;
+import com.sheep.jiuyan.samllsheep.utils.G;
+
+/**
+ * Created by realicing on 2018/4/27.
+ * realicing@sina.com
+ */
+public class DragRelativeLayout extends RelativeLayout {
+    public DragRelativeLayout(Context context) {
+        super(context);
+    }
+
+    public DragRelativeLayout(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public DragRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+
+    float padding = getResources().getDimension(R.dimen.content_padding_10);
+    float moveX;
+    float moveY;
+    private boolean isMove = false;
+    @Override
+    public boolean onTouchEvent(MotionEvent event) {
+        switch (event.getAction()) {
+            case MotionEvent.ACTION_DOWN:
+                isMove = false;
+                moveX = event.getX();
+                moveY = event.getY();
+                break;
+            case MotionEvent.ACTION_MOVE:
+                isMove = true;
+                setX(getX() + (event.getX() - moveX));
+                setY(getY() + (event.getY() - moveY));
+                break;
+            case MotionEvent.ACTION_UP:
+                if (getX() * 2 < G.WIDTH)
+                    setX(padding);
+                else
+                    setX(G.WIDTH - padding - getWidth());
+                if(!isMove){//没有移动过就点击
+                    performClick();
+                }
+                isMove = false;
+                break;
+            case MotionEvent.ACTION_CANCEL:
+                break;
+        }
+
+        return true;
+    }
+}

+ 14 - 0
app/src/main/java/com/sheep/gamegroup/util/ConnectAddress.java

@@ -12,19 +12,33 @@ public enum ConnectAddress {
         public String getAppUrl() {
             return "http://10.8.210.171:8080/";
         }
+        @Override
+        public String getName() {
+            return "张伟";
+        }
     },sheeptest {
 
         @Override
         public String getAppUrl() {
             return "http://test.sheep.kfzs.com/";
         }
+        @Override
+        public String getName() {
+            return "测试服";
+        }
     },sheep {
 
         @Override
         public String getAppUrl() {
             return "http://ss.kfzs.com/";
         }
+
+        @Override
+        public String getName() {
+            return "正式服";
+        }
     };
     public abstract String getAppUrl();
 
+    public abstract String getName();
 }

+ 7 - 8
app/src/main/java/com/sheep/jiuyan/samllsheep/SheepApp.java

@@ -53,14 +53,13 @@ public class SheepApp extends BaseApplication {
         return connectAddress;
     }
 
-    public void setConnectAddress(Activity activity, String flavor) {
-        try {
-            connectAddress = ConnectAddress.valueOf(flavor);
-            SharedPreferences.getInstance().putString("flavor", flavor);
-            quit();
-        } catch (IllegalArgumentException e) {
-            e.printStackTrace();
-        }
+    public void setConnectAddress(ConnectAddress connectAddress) {
+            if(this.connectAddress != connectAddress){
+                SharedPreferences.getInstance().putString("flavor", connectAddress.name());
+                quit();
+            } else {
+                G.showToast("已经是该服务器");
+            }
     }
 
     @Override

+ 14 - 2
app/src/main/res/layout/homepage_act_layout.xml

@@ -41,7 +41,7 @@
     </ScrollView>
     </android.support.v4.widget.SwipeRefreshLayout>
 
-    <RelativeLayout
+    <com.kfzs.duanduan.view.DragRelativeLayout
         android:id="@+id/newbie_task"
         android:layout_width="30dp"
         android:layout_height="wrap_content"
@@ -62,7 +62,19 @@
             android:textSize="13sp"
             android:textColor="#ffffff"
             android:text="新手任务"/>
-    </RelativeLayout>
+    </com.kfzs.duanduan.view.DragRelativeLayout>
+    <TextView
+        android:id="@+id/test_change"
+        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_alignParentTop="true"
+        android:layout_alignParentEnd="true"
+        android:text="切换服务器"
+        android:visibility="visible"/>
     <ImageView
         android:id="@+id/couple_red_packets_iv"
         android:layout_width="match_parent"