drew 6 năm trước cách đây
mục cha
commit
57ef0e0b91

+ 1 - 0
app/build.gradle

@@ -53,4 +53,5 @@ dependencies {
     implementation 'com.squareup.picasso:picasso:2.71828'
     implementation 'com.zy.mocknet:mocknet:1.0'
     implementation 'cat.ereza:customactivityoncrash:2.2.0'
+    implementation 'org.xutils:xutils:3.8.3'
 }

+ 1 - 0
app/src/main/AndroidManifest.xml

@@ -3,6 +3,7 @@
     xmlns:tools="http://schemas.android.com/tools"
     package="com.ht.gate">
 
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 

+ 2 - 1
app/src/main/java/com/ht/gate/ApiService.java

@@ -8,6 +8,7 @@ import java.util.List;
 
 import retrofit2.Call;
 import retrofit2.http.Body;
+import retrofit2.http.GET;
 import retrofit2.http.POST;
 import retrofit2.http.Path;
 import retrofit2.http.Query;
@@ -20,7 +21,7 @@ public interface ApiService {
     Call<List<RoomMeetings>> roomMeetings(@Body QueryMeetingBody body);
 
     //    @POST("employee-center/EmpService/queryEmpInfoByEmpId")
-    @POST("getEmpInfoData")
+    @GET("getEmpInfoData")
     Call<EmpInfo> getEmpInfo(@Query("empId") String empId);
 
 }

+ 1 - 0
app/src/main/java/com/ht/gate/Constants.java

@@ -4,4 +4,5 @@ public interface Constants {
     String PREF_ROOM_INFO = "roomInfo";
     String PREF_PASSWORD = "password";
     String EVENT_MEETINGS = "meetings";
+    String EVENT_NET_STATUS = "netStatus";
 }

+ 26 - 1
app/src/main/java/com/ht/gate/DoorService.java

@@ -5,6 +5,7 @@ import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Binder;
 import android.os.Handler;
+import android.os.Message;
 import android.text.TextUtils;
 import android.util.Log;
 import android.widget.Toast;
@@ -16,11 +17,17 @@ import org.greenrobot.eventbus.EventBus;
 import java.net.DatagramPacket;
 import java.net.DatagramSocket;
 import java.net.InetAddress;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import es.dmoral.toasty.Toasty;
 
 public class DoorService extends Service {
     private static final String TAG = "DoorService";
 
     private static final int MSG_SEND_MSG = 1;
+    private static final int MSG_OPEN_FAIL = 2;
 
     private DatagramSocket socket;
     private InetAddress serverAddress;
@@ -29,6 +36,8 @@ public class DoorService extends Service {
     private SharedPreferences sharedPreferences;
     private String status = "closed";
 
+    private ScheduledExecutorService executorService;
+
     public class DoorBinder extends Binder {
         public DoorService getService() {
             return DoorService.this;
@@ -39,6 +48,8 @@ public class DoorService extends Service {
 
     private Handler mHandler = new Handler(msg -> {
         switch (msg.what) {
+            case MSG_OPEN_FAIL:
+                Toasty.error(getApplicationContext(), "解锁失败,请稍后再试").show();
         }
         return true;
     });
@@ -51,6 +62,14 @@ public class DoorService extends Service {
     public void onCreate() {
         super.onCreate();
         sharedPreferences = getSharedPreferences(SettingsActivity.PREF_NAME_SETTINGS, MODE_PRIVATE);
+        executorService = Executors.newScheduledThreadPool(2);
+        executorService.scheduleAtFixedRate(new Runnable() {
+            @Override
+            public void run() {
+                MyApplication.netAvailable = NetCheckUtil.checkNet(getApplicationContext());
+                EventBus.getDefault().post(Constants.EVENT_NET_STATUS);
+            }
+        }, 0, 5 * 1000, TimeUnit.MILLISECONDS);
         restartSocket();
     }
 
@@ -80,7 +99,7 @@ public class DoorService extends Service {
         new Thread(() -> {
             while (socket != null && !socket.isClosed()) {
                 try {
-                    Thread.sleep(300);
+                    Thread.sleep(100);
                     byte[] data = new byte[1024];
                     DatagramPacket packet = new DatagramPacket(data, data.length);
                     socket.receive(packet);
@@ -88,6 +107,9 @@ public class DoorService extends Service {
                     Log.i(TAG, "UDP result: " + result);
                     EventBus.getDefault().post(result);
                     status = result;
+                    if ("closed".equals(result)) {
+                        mHandler.removeMessages(MSG_OPEN_FAIL);
+                    }
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
@@ -123,6 +145,9 @@ public class DoorService extends Service {
                 }
             }).start();
         }
+        Message msg = new Message();
+        msg.what = MSG_OPEN_FAIL;
+        mHandler.sendMessageDelayed(msg, 10 * 1000);
     }
 
     public String getStatus() {

+ 12 - 5
app/src/main/java/com/ht/gate/MainActivity.java

@@ -69,6 +69,8 @@ public class MainActivity extends AppCompatActivity {
     RecyclerView rvMeetings;
     @BindView(R.id.iv_arrow)
     ImageView ivArrow;
+    @BindView(R.id.tv_net_err)
+    TextView tvNetErr;
     private MyLinearLayoutManager layoutManager;
     private MeetingAdapter adapter;
     private DoorService.DoorBinder mDoorBinder;
@@ -121,11 +123,10 @@ public class MainActivity extends AppCompatActivity {
                         if (mDoorBinder != null) {
                             mDoorBinder.getService().openDoor();
                         }
-                        Toasty.normal(MainActivity.this, "已解锁").show();
-                        SoundUtil.getInstance(MainActivity.this).play(SoundUtil.UNLOCK_SUCCESS);
+//                        Toasty.normal(MainActivity.this, "已解锁").show();
+//                        SoundUtil.getInstance(MainActivity.this).play(SoundUtil.UNLOCK_SUCCESS);
                     } else {
-                        Toasty.normal(MainActivity.this, "密码错误", ContextCompat.getDrawable(MainActivity.this, R.mipmap.toast_icon_warn_fill))
-                                .show();
+                        Toasty.error(MainActivity.this, "密码错误").show();
                         SoundUtil.getInstance(MainActivity.this).play(SoundUtil.UNLOCK_FAIL);
                     }
                     Message msg = new Message();
@@ -181,9 +182,12 @@ public class MainActivity extends AppCompatActivity {
 
     @Subscribe(threadMode = ThreadMode.MAIN)
     public void onMessageEvent(String msg) {
-        updateStatus();
         if (Constants.EVENT_MEETINGS.equals(msg)) {
             updateMeetings();
+        } else if (Constants.EVENT_NET_STATUS.equals(msg)) {
+            tvNetErr.setVisibility(MyApplication.netAvailable ? View.GONE : View.VISIBLE);
+        } else {
+            updateStatus();
         }
     }
 
@@ -239,6 +243,9 @@ public class MainActivity extends AppCompatActivity {
         if (mDoorBinder != null) {
             String status = mDoorBinder.getService().getStatus();
             if ("opened".equals(status)) {
+                if (!unlockView.isUnlocked()) {
+                    SoundUtil.getInstance(MainActivity.this).play(SoundUtil.UNLOCK_SUCCESS);
+                }
                 unlockView.setUnlocked(true);
             } else if ("closed".equals(status)) {
                 unlockView.setUnlocked(false);

+ 5 - 0
app/src/main/java/com/ht/gate/MyApplication.java

@@ -14,6 +14,7 @@ import com.ht.gate.domain.RoomMeetings;
 import com.squareup.picasso.Picasso;
 
 import org.greenrobot.eventbus.EventBus;
+import org.xutils.x;
 
 import java.io.IOException;
 import java.text.ParseException;
@@ -47,6 +48,8 @@ public class MyApplication extends Application {
     private ScheduledExecutorService executorService;
     private SharedPreferences sharedPreferences;
 
+    public static boolean netAvailable = true;
+
 
     public static void setPassword(Context context, String password) {
         MyApplication.password = password;
@@ -58,6 +61,8 @@ public class MyApplication extends Application {
     @Override
     public void onCreate() {
         super.onCreate();
+        x.Ext.init(this);
+        x.Ext.setDebug(BuildConfig.DEBUG);
         executorService = Executors.newScheduledThreadPool(2);
         sharedPreferences = getSharedPreferences(SettingsActivity.PREF_NAME_SETTINGS, MODE_PRIVATE);
         roomInfo = Room.fromBase64(sharedPreferences.getString(Constants.PREF_ROOM_INFO, null));

+ 4 - 0
app/src/main/java/com/ht/gate/UnlockView.java

@@ -124,6 +124,10 @@ public class UnlockView extends View {
         postInvalidate();
     }
 
+    public boolean isUnlocked() {
+        return unlocked;
+    }
+
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);

+ 44 - 16
app/src/main/java/com/ht/gate/WelcomeActivity.java

@@ -1,6 +1,7 @@
 package com.ht.gate;
 
 import androidx.appcompat.app.AppCompatActivity;
+import androidx.constraintlayout.widget.ConstraintLayout;
 import androidx.core.content.ContextCompat;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
@@ -26,6 +27,7 @@ import android.view.animation.Animation;
 import android.view.animation.LinearInterpolator;
 import android.view.animation.RotateAnimation;
 import android.widget.ImageView;
+import android.widget.LinearLayout;
 import android.widget.RelativeLayout;
 import android.widget.TextView;
 
@@ -42,6 +44,7 @@ import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.Optional;
 import java.util.Timer;
 import java.util.TimerTask;
 
@@ -93,6 +96,10 @@ public class WelcomeActivity extends AppCompatActivity {
     TextView tvJobYear;
     @BindView(R.id.iv_arrow)
     ImageView ivArrow;
+    @BindView(R.id.tv_net_err)
+    TextView tvNetErr;
+    @BindView(R.id.ll_info)
+    LinearLayout llInfo;
 
     private MeetingAdapter adapter;
     private DoorService.DoorBinder mDoorBinder;
@@ -151,12 +158,11 @@ public class WelcomeActivity extends AppCompatActivity {
                         if (mDoorBinder != null) {
                             mDoorBinder.getService().openDoor();
                         }
-                        SoundUtil.getInstance(WelcomeActivity.this).play(SoundUtil.UNLOCK_SUCCESS);
-                        Toasty.normal(WelcomeActivity.this, "已解锁").show();
+//                        SoundUtil.getInstance(WelcomeActivity.this).play(SoundUtil.UNLOCK_SUCCESS);
+//                        Toasty.normal(WelcomeActivity.this, "已解锁").show();
                     } else {
                         SoundUtil.getInstance(WelcomeActivity.this).play(SoundUtil.UNLOCK_FAIL);
-                        Toasty.normal(WelcomeActivity.this, "密码错误", ContextCompat.getDrawable(WelcomeActivity.this, R.mipmap.toast_icon_warn_fill))
-                                .show();
+                        Toasty.error(WelcomeActivity.this, "密码错误").show();
                     }
                     Message msg = new Message();
                     msg.what = HIDE_BAR;
@@ -177,6 +183,7 @@ public class WelcomeActivity extends AppCompatActivity {
         if (empInfo.getHalfBodyPhoto() != null) {
             Picasso.get().load(empInfo.getHalfBodyPhoto()).placeholder(R.mipmap.portrait).into(ivPortrait);
         }
+
         if (TextUtils.isEmpty(meeting.getWelcomeText())) {
             tvWelcome.setText("欢迎");
             tvWelcome.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 63, getResources().getDisplayMetrics()));
@@ -195,6 +202,13 @@ public class WelcomeActivity extends AppCompatActivity {
                 tvWelcome.setTextSize(TypedValue.COMPLEX_UNIT_SP, 33);
             }
         }
+        if (Optional.ofNullable(meeting.getWelcomeText()).orElse("").length() > 4) {
+            ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) llInfo.getLayoutParams();
+            params.setMargins((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 44, getResources().getDisplayMetrics()),
+                    (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 47, getResources().getDisplayMetrics()),
+                    0, 0);
+            llInfo.setLayoutParams(params);
+        }
         tvAdviserName.setText(empInfo.getUserName());
         if (empInfo.getDescription() == null) {
             tvIntro.setText("20年从业经验,经历多次牛熊转换磨练,对市场具有一定的敏感度,注重价值投资,资产配置,秉承专业的投资理念服务客户");
@@ -216,18 +230,26 @@ public class WelcomeActivity extends AppCompatActivity {
             rlJobYear.setVisibility(View.GONE);
         } else {
             tvCertNo.setText("执业证书编号:" + empInfo.getCertifications().get(0).getCertificateNumber());
-            int year;
-            if (empInfo.getCertifications().get(0).getCertificateAcquisitionTime() == 0) {
+        }
+
+        int year = 0;
+        try {
+            if (TextUtils.isEmpty(empInfo.getEntryTime())) {
                 year = 0;
             } else {
-                year = (int) ((System.currentTimeMillis() - empInfo.getCertifications().get(0).getCertificateAcquisitionTime()) / 1000 / 60 / 60 / 24 / 365);
-            }
-            if (year < 3) {
-                rlJobYear.setVisibility(View.GONE);
-            } else {
-                tvJobYear.setText(year + "年");
+                year = (int) ((System.currentTimeMillis() - Long.parseLong(empInfo.getEntryTime())) / 1000 / 60 / 60 / 24 / 365);
             }
+
+        } catch (Exception e) {
+
         }
+        if (year < 3) {
+            rlJobYear.setVisibility(View.GONE);
+        } else {
+            tvJobYear.setText(year + "年");
+        }
+
+
         scheduleFinish();
     }
 
@@ -290,12 +312,15 @@ public class WelcomeActivity extends AppCompatActivity {
 
     @Subscribe(threadMode = ThreadMode.MAIN)
     public void onMessageEvent(String msg) {
-        updateStatus();
         if ("closed".equals(msg)) {
-            finish();
-        }
-        if (Constants.EVENT_MEETINGS.equals(msg)) {
+//            finish();
+            updateStatus();
+        } else if (Constants.EVENT_MEETINGS.equals(msg)) {
             updateMeetings();
+        } else if (Constants.EVENT_NET_STATUS.equals(msg)) {
+            tvNetErr.setVisibility(MyApplication.netAvailable ? View.GONE : View.VISIBLE);
+        } else {
+            updateStatus();
         }
     }
 
@@ -321,6 +346,9 @@ public class WelcomeActivity extends AppCompatActivity {
         if (mDoorBinder != null) {
             String status = mDoorBinder.getService().getStatus();
             if ("opened".equals(status)) {
+                if (!unlockView.isUnlocked()) {
+                    SoundUtil.getInstance(WelcomeActivity.this).play(SoundUtil.UNLOCK_SUCCESS);
+                }
                 unlockView.setUnlocked(true);
             } else if ("closed".equals(status)) {
                 unlockView.setUnlocked(false);

+ 26 - 0
app/src/main/res/layout/activity_main.xml

@@ -6,10 +6,35 @@
     android:layout_height="match_parent"
     tools:mContext=".MainActivity">
 
+
     <com.ht.gate.VideoBackground
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
 
+    <com.ht.gate.TimeView
+        android:id="@+id/tv_time"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginLeft="8dp"
+        android:layout_marginTop="8dp"
+        android:textColor="@color/colorPrimary"
+        android:textSize="14sp"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <TextView
+        android:id="@+id/tv_net_err"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="网络连接异常,请检查网络设置"
+        android:textColor="#F56C6C"
+        android:textSize="12sp"
+        android:visibility="gone"
+        app:layout_constraintBottom_toBottomOf="@id/tv_time"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toRightOf="parent"
+        app:layout_constraintTop_toTopOf="@id/tv_time" />
+
     <ImageButton
         android:id="@+id/btn_settings"
         android:layout_width="wrap_content"
@@ -22,6 +47,7 @@
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
 
+
     <ImageView
         android:id="@+id/iv_logo"
         android:layout_width="162dp"

+ 25 - 0
app/src/main/res/layout/activity_welcome.xml

@@ -19,6 +19,30 @@
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toTopOf="parent">
 
+        <com.ht.gate.TimeView
+            android:id="@+id/tv_time"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginLeft="8dp"
+            android:layout_marginTop="8dp"
+            android:textColor="#222639"
+            android:textSize="14sp"
+            app:layout_constraintLeft_toLeftOf="parent"
+            app:layout_constraintTop_toTopOf="parent" />
+
+        <TextView
+            android:id="@+id/tv_net_err"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="网络连接异常,请检查网络设置"
+            android:textColor="#F56C6C"
+            android:textSize="12sp"
+            android:visibility="gone"
+            app:layout_constraintBottom_toBottomOf="@id/tv_time"
+            app:layout_constraintLeft_toLeftOf="parent"
+            app:layout_constraintRight_toRightOf="parent"
+            app:layout_constraintTop_toTopOf="@id/tv_time" />
+
         <TextView
             android:id="@+id/tv_room_name"
             android:layout_width="wrap_content"
@@ -91,6 +115,7 @@
             app:layout_constraintRight_toRightOf="parent" />
 
         <LinearLayout
+            android:id="@+id/ll_info"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginLeft="44dp"