浏览代码

完成定位更新,完成自动登录,禁止最近活跃群滚动

Junqin Wang 8 年之前
父节点
当前提交
c4a28f4d2b

+ 25 - 18
app/res/layout/area_groups_list_fragment.xml

@@ -1,9 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_height="match_parent"
     android:layout_width="wrap_content"
-    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_height="match_parent"
     android:fitsSystemWindows="false">
 
     <LinearLayout
@@ -20,7 +19,7 @@
             android:background="@color/grey_light"
             android:gravity="center"
             android:text="请输入群名字"
-            android:visibility="gone"/>
+            android:visibility="gone" />
 
         <android.support.v7.widget.RecyclerView
             android:id="@+id/area_groups_recyclerview"
@@ -86,25 +85,33 @@
                 android:text="用户创建聊天室" />
         </LinearLayout>
 
-        <View style="@style/horizontal_light_thin_divider" />
-
-        <TextView
+        <LinearLayout
             android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:background="@color/white"
-            android:padding="10dp"
-            android:text="最近活跃的群" />
+            android:layout_height="match_parent"
+            android:orientation="vertical">
 
-        <View style="@style/horizontal_light_thin_divider" />
+            <View style="@style/horizontal_light_thin_divider" />
 
-        <android.support.v7.widget.RecyclerView
-            android:id="@+id/area_groups_nearby_recyclerview"
-            android:layout_width="match_parent"
-            android:layout_height="400dp"
-            android:background="@color/white" />
+            <TextView
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:background="@color/white"
+                android:padding="10dp"
+                android:text="最近活跃的群" />
 
-        <View style="@style/horizontal_light_thin_divider"
-            android:layout_marginTop="30dp"/>
+            <View style="@style/horizontal_light_thin_divider" />
+
+            <android.support.v7.widget.RecyclerView
+                android:id="@+id/area_groups_nearby_recyclerview"
+                android:layout_width="match_parent"
+                android:layout_height="700dp"
+                android:background="@color/white" />
+
+            <View
+                style="@style/horizontal_light_thin_divider"
+                android:layout_marginTop="30dp" />
+
+        </LinearLayout>
 
     </LinearLayout>
 </ScrollView>

+ 78 - 0
app/src/com/sheishuo/app/common/util/data/SPHelper.java

@@ -0,0 +1,78 @@
+package com.sheishuo.app.common.util.data;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+
+/**
+ * Created by KN on 2017/8/17.
+ */
+
+public class SPHelper {
+    /**
+     * 保存在手机里面的文件名
+     */
+    private static final String FILE_NAME = "share_date";
+
+
+    /**
+     * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
+     * @param context
+     * @param key
+     * @param object
+     */
+    public static void setParam(Context context , String key, Object object){
+
+        String type = object.getClass().getSimpleName();
+        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
+        SharedPreferences.Editor editor = sp.edit();
+
+        if("String".equals(type)){
+            editor.putString(key, (String)object);
+        }
+        else if("Integer".equals(type)){
+            editor.putInt(key, (Integer)object);
+        }
+        else if("Boolean".equals(type)){
+            editor.putBoolean(key, (Boolean)object);
+        }
+        else if("Float".equals(type)){
+            editor.putFloat(key, (Float)object);
+        }
+        else if("Long".equals(type)){
+            editor.putLong(key, (Long)object);
+        }
+
+        editor.commit();
+    }
+
+
+    /**
+     * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
+     * @param context
+     * @param key
+     * @param defaultObject
+     * @return
+     */
+    public static Object getParam(Context context , String key, Object defaultObject){
+        String type = defaultObject.getClass().getSimpleName();
+        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
+
+        if("String".equals(type)){
+            return sp.getString(key, (String)defaultObject);
+        }
+        else if("Integer".equals(type)){
+            return sp.getInt(key, (Integer)defaultObject);
+        }
+        else if("Boolean".equals(type)){
+            return sp.getBoolean(key, (Boolean)defaultObject);
+        }
+        else if("Float".equals(type)){
+            return sp.getFloat(key, (Float)defaultObject);
+        }
+        else if("Long".equals(type)){
+            return sp.getLong(key, (Long)defaultObject);
+        }
+
+        return null;
+    }
+}

+ 1 - 0
app/src/com/sheishuo/app/common/util/net/INet.java

@@ -13,6 +13,7 @@ public interface INet {
     void baseQuery();
     void post(String url,FormBody body,ResponseCallback callback);
     void post(String url, RequestBody body,ResponseCallback callback);
+    void updateLatitudeAndLongitude(ResponseCallback callback);
     void updateLocation(String province,String city,String district,ResponseCallback callback);
     void loginQuery(String mobile,String pwd,ResponseCallback callback);
     void getGiftList(ResponseCallback callback);

+ 50 - 0
app/src/com/sheishuo/app/common/util/net/NetImpl.java

@@ -1,9 +1,13 @@
 package com.sheishuo.app.common.util.net;
 
+import android.util.Log;
+
 import com.google.gson.Gson;
 import com.sheishuo.app.cache.AccountCache;
+import com.sheishuo.app.common.beans.CircleBean;
 import com.sheishuo.app.common.beans.GiftBean;
 import com.sheishuo.app.common.beans.SocialBean;
+import com.sheishuo.app.common.util.location.LocationHelper;
 import com.sheishuo.app.login.beans.LoginBean;
 
 import org.json.JSONException;
@@ -81,6 +85,52 @@ public class NetImpl implements INet {
     }
 
     @Override
+    public void updateLatitudeAndLongitude(final ResponseCallback callback) {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    FormBody body = new FormBody.Builder()
+                            .add("id",AccountCache.getAccount().getId())
+                            .add("latitude", LocationHelper.getLatitude())
+                            .add("longitude",LocationHelper.getLongitude())
+                            .build();
+
+                    Request request = new Request.Builder()
+                            .post(body)
+                            .url(NetInfo.UPDATE_LATITUDE_AND_LONGITUDE)
+                            .build();
+
+                    Response response = client.newCall(request).execute();
+
+                    int responseCode = response.code();
+                    if (200 == responseCode){
+                        Log.e("code",""+responseCode);
+                        LoginBean.DBean account = AccountCache.getAccount();
+                        JSONObject data = new JSONObject(response.body().string()).getJSONObject("d");
+                        if (data.getBoolean("update")){
+                            account.setProvince(data.getString("province"));
+                            account.setCity(data.getString("city"));
+                            account.setDistrict(data.getString("district"));
+                            account.setProvince_room_id(data.getString("province_room_id"));
+                            account.setCity_room_id(data.getString("city_room_id"));
+                            account.setDistrict_room_id(data.getString("district_room_id"));
+                            callback.onSuccess(null);
+                        }
+                    }else {
+                        callback.onFailed();
+                    }
+
+
+                }catch (Exception e){
+                    e.printStackTrace();
+                    callback.onFailed();
+                }
+            }
+        }).start();
+    }
+
+    @Override
     public void updateLocation(final String province, final String city, final String district, final ResponseCallback callback) {
         new Thread(new Runnable() {
             @Override

+ 1 - 0
app/src/com/sheishuo/app/common/util/net/NetInfo.java

@@ -21,4 +21,5 @@ public class NetInfo {
     public final static String GET_SOCIAL_INFO = INDEX + "?m=who&c=index&a=gift_list";
     public final static String GET_CITY_DATA = "https://junqin.wang/API/sheishuo/cityData.json";
     public final static String RESET_LOCATION_BY_DISTRICT= INDEX + "?m=who&c=index&a=reset_location";
+    public final static String UPDATE_LATITUDE_AND_LONGITUDE = INDEX + "?m=who&c=index&a=coordinate_2_location";
 }

+ 6 - 0
app/src/com/sheishuo/app/login/LoginActivity.java

@@ -1,5 +1,6 @@
 package com.sheishuo.app.login;
 
+import android.app.backup.SharedPreferencesBackupHelper;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
@@ -12,6 +13,7 @@ import android.widget.Toast;
 
 import com.sheishuo.app.cache.AccountCache;
 import com.sheishuo.app.R;
+import com.sheishuo.app.common.util.data.SPHelper;
 import com.sheishuo.app.common.util.net.INet;
 import com.sheishuo.app.common.util.net.NetImpl;
 import com.sheishuo.app.common.util.net.ResponseCallback;
@@ -95,6 +97,10 @@ public class LoginActivity extends SheishuoUI implements View.OnClickListener{
             @Override
             public void onSuccess(Object object) {
                 LoginBean bean = (LoginBean)object;
+                //存入SP用于自动登录
+                SPHelper.setParam(context,"mobile",tel);
+                SPHelper.setParam(context,"pwd",pwd);
+
 
                 //将用户信息放入缓存
                 AccountCache.setCache(bean.getD());

+ 47 - 1
app/src/com/sheishuo/app/login/LoginAndRegActivity.java

@@ -7,9 +7,16 @@ import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
+import android.widget.Toast;
 
 import com.netease.nim.uikit.permission.MPermission;
 import com.sheishuo.app.R;
+import com.sheishuo.app.cache.AccountCache;
+import com.sheishuo.app.common.util.data.SPHelper;
+import com.sheishuo.app.common.util.net.NetImpl;
+import com.sheishuo.app.common.util.net.ResponseCallback;
+import com.sheishuo.app.login.beans.LoginBean;
+import com.sheishuo.app.login.helper.LoginHelper;
 import com.sheishuo.app.uikit_implements.SheishuoUI;
 
 public class LoginAndRegActivity extends SheishuoUI implements View.OnClickListener{
@@ -49,8 +56,8 @@ public class LoginAndRegActivity extends SheishuoUI implements View.OnClickListe
 //        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 //                WindowManager.LayoutParams.FLAG_FULLSCREEN);
         setContentView(R.layout.login_and_reg_layout);
-        init();
         requestBasicPermission();
+        init();
     }
 
 
@@ -59,6 +66,45 @@ public class LoginAndRegActivity extends SheishuoUI implements View.OnClickListe
         regBtn = findView(R.id.btn_register);
         loginBtn.setOnClickListener(this);
         regBtn.setOnClickListener(this);
+
+        final String mobile = (String) SPHelper.getParam(context,"mobile","");
+        final String pwd = (String) SPHelper.getParam(context,"pwd","");
+        if (!(mobile.isEmpty() || pwd.isEmpty())){
+            showToast(context,"自动登录中,请稍后");
+            new NetImpl().loginQuery(mobile, pwd, new ResponseCallback() {
+                @Override
+                public void onSuccess(Object object) {
+                    LoginBean bean = (LoginBean)object;
+                    //存入SP用于自动登录
+                    SPHelper.setParam(context,"mobile",mobile);
+                    SPHelper.setParam(context,"pwd",pwd);
+
+
+                    //将用户信息放入缓存
+                    AccountCache.setCache(bean.getD());
+                    AccountCache.sync();
+
+
+
+                    //登录NIM服务
+                    new LoginHelper(context,bean).loginToNIM();
+
+
+                }
+
+                @Override
+                public void onFailed() {
+                    getHandler().post(new Runnable() {
+                        @Override
+                        public void run() {
+                            Toast.makeText(context,"登录失败",Toast.LENGTH_SHORT).show();
+                            loginBtn.setEnabled(true);
+                            loginBtn.setText("登录");
+                        }
+                    });
+                }
+            });
+        }
     }
     protected <T extends View> T findView(int resId) {
         return (T) (findViewById(resId));

+ 5 - 0
app/src/com/sheishuo/app/login/helper/LoginHelper.java

@@ -55,6 +55,8 @@ public class LoginHelper {
                     SheishuoCache.setAccount(param.getAccount());
                     saveLoginInfo(param.getAccount(), param.getToken());
 
+
+
                     // 初始化消息提醒配置
                     initNotificationConfig();
 
@@ -96,4 +98,7 @@ public class LoginHelper {
         NIMClient.updateStatusBarNotificationConfig(statusBarNotificationConfig);
     }
 
+
+
+
 }

+ 28 - 0
app/src/com/sheishuo/app/main/fragment/AreaGroupsFragment.java

@@ -20,6 +20,7 @@ import com.sheishuo.app.cache.AccountCache;
 import com.sheishuo.app.cache.GiftCache;
 import com.sheishuo.app.common.beans.GiftBean;
 import com.sheishuo.app.common.beans.NearbyGroupsBean;
+import com.sheishuo.app.common.util.location.LocationHelper;
 import com.sheishuo.app.common.util.net.INet;
 import com.sheishuo.app.common.util.net.NetImpl;
 import com.sheishuo.app.common.util.net.NetInfo;
@@ -78,6 +79,7 @@ public class AreaGroupsFragment extends MainTabFragment {
 
 
     void init(){
+        updateLocation();
         loadGroups();
         initToolbar();
         loadGiftCache();
@@ -123,6 +125,32 @@ public class AreaGroupsFragment extends MainTabFragment {
     }
 
 
+    //更新地理位置
+    public void updateLocation(){
+        net.updateLatitudeAndLongitude(new ResponseCallback() {
+            @Override
+            public void onSuccess(Object object) {
+                getHandler().post(new Runnable() {
+                    @Override
+                    public void run() {
+                        loadGroups();
+                        location = AccountCache.getAccount().getCity()
+                                + AccountCache.getAccount().getDistrict();
+                        if (location.length() > 6) {
+                            location = location.substring(0, 6) + "...";
+                        }
+
+                        toolbar.setLeftText(location);
+                    }
+                });
+            }
+
+            @Override
+            public void onFailed() {
+                showToast("更新地理位置失败,请检查定位权限");
+            }
+        });
+    }
 
     //加载聊天室信息
     void loadGroups() {

+ 0 - 9
app/src/com/sheishuo/app/main/fragment/SessionListFragment.java

@@ -172,15 +172,6 @@ public class SessionListFragment extends MainTabFragment {
             });
         }
 
-//        toolbar.getIcoLayout().setVisibility(View.VISIBLE);
-//        ImageView contactsImg = new ImageView(getActivity());
-//        ImageView addFriendsImg = new ImageView(getActivity());
-//
-//        Glide.with(getActivity()).load(R.drawable.friends_contacts_list_ico).into(contactsImg);
-//        Glide.with(getActivity()).load(R.drawable.friends_add_ico).into(addFriendsImg);
-//
-//        //toolbar.addView(contactsImg);
-//        toolbar.addView(addFriendsImg);
 
         parent.setSupportActionBar(toolbar);
 

+ 4 - 2
uikit/res/layout/nim_message_activity_text_layout.xml

@@ -21,7 +21,8 @@
             android:layout_height="wrap_content"
             android:background="@drawable/nim_message_button_bottom_audio_selector"
             android:contentDescription="@string/empty"
-            android:scaleType="center" />
+            android:scaleType="center"
+            android:visibility="gone"/>
 
         <ImageView
             android:id="@+id/buttonTextMessage"
@@ -30,7 +31,8 @@
             android:layout_gravity="center_vertical"
             android:background="@drawable/nim_message_button_bottom_text_selector"
             android:contentDescription="@string/empty"
-            android:scaleType="center" />
+            android:scaleType="center"
+            android:visibility="gone"/>
     </FrameLayout>
 
     <FrameLayout

+ 2 - 1
uikit/src/com/netease/nim/uikit/recent/holder/TeamRecentViewHolder.java

@@ -7,6 +7,7 @@ import com.netease.nim.uikit.cache.TeamDataCache;
 import com.netease.nim.uikit.common.ui.recyclerview.adapter.BaseQuickAdapter;
 import com.netease.nim.uikit.recent.TeamMemberAitHelper;
 import com.netease.nimlib.sdk.msg.attachment.NotificationAttachment;
+import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
 import com.netease.nimlib.sdk.msg.model.RecentContact;
 
 public class TeamRecentViewHolder extends CommonRecentViewHolder {
@@ -17,8 +18,8 @@ public class TeamRecentViewHolder extends CommonRecentViewHolder {
 
     @Override
     protected String getContent(RecentContact recent) {
-        String content = descOfMsg(recent);
 
+        String content = descOfMsg(recent);
         String fromId = recent.getFromAccount();
         if (!TextUtils.isEmpty(fromId)
                 && !fromId.equals(NimUIKit.getAccount())