liuzhenxing 3 жил өмнө
parent
commit
43f46c8c08

+ 74 - 32
app/src/main/java/com/xplora/xplauncher/activity/MainActivity.java

@@ -1,9 +1,14 @@
 package com.xplora.xplauncher.activity;
 
+import androidx.annotation.NonNull;
 import androidx.viewpager.widget.ViewPager;
 
 import android.content.ComponentName;
 import android.content.Intent;
+import android.database.ContentObserver;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
 import android.view.View;
 
 import com.xplora.xplauncher.R;
@@ -33,7 +38,6 @@ public class MainActivity extends BaseActivity {
     private int mAppTitleStatus = 1;
     private int mDeleteBtnStatus = 0;
     private TimeBroadcastReceiver mTimeReceiver = null;
-    private Object ContactsPagerOnClickListener;
 
     @Override
     protected void onCreateBase() {
@@ -44,7 +48,8 @@ public class MainActivity extends BaseActivity {
     protected void initDataBase() {
         super.initDataBase();
 
-        //DataManager.initData(this);
+        //监听数据库
+        DataManager.registerContentObserver(new RecentContentObserver(mHandler), new ContactContentObserver(mHandler));
     }
 
     @Override
@@ -52,8 +57,6 @@ public class MainActivity extends BaseActivity {
         super.initViewBase();
 
         initViewPager();
-//        refreshWeather();
-//        refreshTime();
         initBroadcast();
     }
 
@@ -74,34 +77,7 @@ public class MainActivity extends BaseActivity {
         mViewPagerAdapter = new ViewPagerAdapter(mPagesDatas);//创建适配器对象
         mViewPager.setAdapter(mViewPagerAdapter);//设置适配器
         mViewPager.setCurrentItem(Constant.HOME_INDEX);
-
-        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
-            @Override
-            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
-            }
-
-            @Override
-            public void onPageSelected(int position) {
-                if (position == 0) {
-                    ContactsPager contactPager = (ContactsPager)mPagerViews.get(0);
-                    if (contactPager != null && !contactPager.getIsSetData()) {
-                        contactPager.setDataList(DataManager.loadRecent(), DataManager.loadContacts());
-                    }
-                }
-
-                //时间
-                HomePager homeView = (HomePager)mPagerViews.get(1);
-                if (position == 1) {
-                    homeView.refreshTime();
-                }else {
-                    homeView.pauseSecondAnim();
-                }
-            }
-
-            @Override
-            public void onPageScrollStateChanged(int state) {
-            }
-        });
+        mViewPager.addOnPageChangeListener(new ViewPageChangeListener());
     }
 
     private void initContactsPager() {
@@ -144,6 +120,34 @@ public class MainActivity extends BaseActivity {
     }
 
     // ======================== Listener =================================
+    public class ViewPageChangeListener implements ViewPager.OnPageChangeListener {
+        @Override
+        public void onPageSelected(int position) {
+            //第一次刷新联系人信息
+            if (position == 0) {
+                ContactsPager contactPager = (ContactsPager)mPagerViews.get(0);
+                if (contactPager != null && !contactPager.getIsSetData()) {
+                    contactPager.setDataList(DataManager.loadRecent(), DataManager.loadContacts());
+                }
+            }
+
+            //时间
+            HomePager homeView = (HomePager)mPagerViews.get(1);
+            if (position == 1) {
+                homeView.refreshTime();
+            }else {
+                homeView.pauseSecondAnim();
+            }
+        }
+
+        @Override
+        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+        }
+        @Override
+        public void onPageScrollStateChanged(int state) {
+        }
+    }
+
     public class HomePagerOnLongClick implements View.OnLongClickListener {
         @Override
         public boolean onLongClick(View v) {
@@ -296,6 +300,44 @@ public class MainActivity extends BaseActivity {
         //mViewPagerAdapter.notifyDataSetChanged();
     }
 
+    // ======================== 监听数据库 =======================
+    public class RecentContentObserver extends ContentObserver {
+        public RecentContentObserver(Handler handler) {
+            super(handler);
+        }
+
+        @Override
+        public void onChange(boolean selfChange) {
+            ContactsPager contactPager = (ContactsPager)mPagerViews.get(0);
+            if (contactPager != null) {
+                contactPager.setContactsList(DataManager.loadContacts());
+            }
+            super.onChange(selfChange);
+        }
+    }
+
+    public class ContactContentObserver extends ContentObserver {
+        public ContactContentObserver(Handler handler) {
+            super(handler);
+        }
+
+        @Override
+        public void onChange(boolean selfChange) {
+            ContactsPager contactPager = (ContactsPager)mPagerViews.get(0);
+            if (contactPager != null) {
+                contactPager.setRecentList(DataManager.loadRecent());
+            }
+            super.onChange(selfChange);
+        }
+    }
+
+    Handler mHandler = new Handler(Looper.myLooper()) {
+        @Override
+        public void handleMessage(@NonNull Message msg) {
+            super.handleMessage(msg);
+        }
+    };
+
     // ======================== 广播 =======================
     public void initBroadcast() {
         //时间广播, 1分钟一次

+ 18 - 8
app/src/main/java/com/xplora/xplauncher/data/DataManager.java

@@ -5,9 +5,12 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
+import android.database.ContentObserver;
 import android.graphics.drawable.Drawable;
+import android.os.Handler;
 
 import com.xplora.xplauncher.R;
+import com.xplora.xplauncher.activity.BaseActivity;
 import com.xplora.xplauncher.model.AppModel;
 import com.xplora.xplauncher.model.ContactModel;
 import com.xplora.xplauncher.utils.Constant;
@@ -24,8 +27,6 @@ public class DataManager extends Application {
     private static int mDeleteStatus = 0;
     private static List<AppModel> mInstalledAppsList = new ArrayList<>();
     private static List<AppModel> mQuickAppsList = new ArrayList<>(Constant.COUNT_QUICK_APP);
-    private static List<ContactModel> mRecentList = new ArrayList<>();
-    private static List<ContactModel> mContactList = new ArrayList<>();
 
     @Override
     public void onCreate() {
@@ -47,6 +48,13 @@ public class DataManager extends Application {
         loadWeatherIndex();
     }
 
+    public static void registerContentObserver(ContentObserver recentObserver, ContentObserver contactObserver) {
+        if (Constant.DEBUG)
+            return;
+        sContext.getContentResolver().registerContentObserver(MetaData.TABLE_URI_RECENT, true, recentObserver);
+        sContext.getContentResolver().registerContentObserver(MetaData.TABLE_URI_CONTACT, true, contactObserver);
+    }
+
     public static List<AppModel> loadInstalledApps() {
         PackageManager pm = sContext.getPackageManager();
         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
@@ -68,30 +76,32 @@ public class DataManager extends Application {
 
     public static List<ContactModel> loadRecent() {
         if (Constant.DEBUG) {
+            List<ContactModel> recentList = new ArrayList<>();
             for (int i = 0; i < 4; i++) {
                 ContactModel model = new ContactModel();
                 model.setName("recent" + i);
                 model.setCallType(i);
                 model.setCallTime(0);
-                mRecentList.add(model);
+                recentList.add(model);
             }
+            return recentList;
         } else {
-            mRecentList = DatabaseHelper.queryRecent(sContext);
+            return DatabaseHelper.queryRecent(sContext);
         }
-        return mRecentList;
     }
 
     public static List<ContactModel> loadContacts() {
         if (Constant.DEBUG) {
+            List<ContactModel> contactList = new ArrayList<>();
             for (int i = 0; i < 6; i++) {
                 ContactModel model = new ContactModel();
                 model.setName("name" + i);
-                mContactList.add(model);
+                contactList.add(model);
             }
+            return contactList;
         } else {
-            mContactList = DatabaseHelper.queryContacts(sContext);
+            return DatabaseHelper.queryContacts(sContext);
         }
-        return mContactList;
     }
 
     public void loadSetting() {

+ 1 - 1
app/src/main/java/com/xplora/xplauncher/data/MetaData.java

@@ -3,7 +3,7 @@ package com.xplora.xplauncher.data;
 import android.net.Uri;
 
 public class MetaData {
-    private static final String AUTHORITIES = "com.xplora .WatchCommonProvider";
+    private static final String AUTHORITIES = "com.xplora.WatchCommonProvider";
     private static final String CONTENT_URI = "content://" + AUTHORITIES + "/";
 
     private static final String TABLE_NAME_RECENT = "recent";

+ 30 - 33
app/src/main/java/com/xplora/xplauncher/view/ContactsPager.java

@@ -22,7 +22,7 @@ import java.util.List;
 public class ContactsPager extends BasePager {
     private final Context mContext;
     private boolean mIsSetData = false;
-    private ContactsRecyclerAdapter mRecyclerAdapter;
+    private ContactsRecyclerAdapter.ContactItemOnClickListener mListener;
     private List<ContactModel> mRecentList = new ArrayList<>();
     private List<ContactModel> mContactsList = new ArrayList<>();
 
@@ -33,34 +33,18 @@ public class ContactsPager extends BasePager {
     public ContactsPager(Context context, ContactsRecyclerAdapter.ContactItemOnClickListener listener) {
         super(context);
         mContext = context;
+        mListener = listener;
 
         mBaseView = LayoutInflater.from(mContext).inflate(R.layout.view_screen_contacts, null);
         mRecyclerView = (RecyclerView)mBaseView.findViewById(R.id.recyclerView);
         mRecentButton = (Button)mBaseView.findViewById(R.id.button_recent);
         mContactButton = (Button)mBaseView.findViewById(R.id.button_contact);
-
-        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(mContext, 1);
-        mRecyclerView.setHasFixedSize(true);
-        mRecyclerView.setLayoutManager(mLayoutManager);
-
-        mRecyclerAdapter = new ContactsRecyclerAdapter(mContext, 0, new ArrayList<>());
-        mRecyclerAdapter.setListOnClickListener(listener);
-        mRecyclerView.setAdapter(mRecyclerAdapter);
-
-        setButtonStatus(0);
         mRecentButton.setOnClickListener(new View.OnClickListener() {
             @SuppressLint("NotifyDataSetChanged")
             @Override
             public void onClick(View v) {
                 setButtonStatus(0);
-
-                RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(mContext, 1);
-                mRecyclerView.setHasFixedSize(true);
-                mRecyclerView.setLayoutManager(mLayoutManager);
-
-                mRecyclerAdapter = new ContactsRecyclerAdapter(mContext, 0, mRecentList);
-                mRecyclerAdapter.setListOnClickListener(listener);
-                mRecyclerView.setAdapter(mRecyclerAdapter);
+                refreshView(0);
             }
         });
         mContactButton.setOnClickListener(new View.OnClickListener() {
@@ -68,16 +52,12 @@ public class ContactsPager extends BasePager {
             @Override
             public void onClick(View v) {
                 setButtonStatus(1);
-
-                RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(mContext, 2);
-                mRecyclerView.setHasFixedSize(true);
-                mRecyclerView.setLayoutManager(mLayoutManager);
-
-                mRecyclerAdapter = new ContactsRecyclerAdapter(mContext, 1, mContactsList);
-                mRecyclerAdapter.setListOnClickListener(listener);
-                mRecyclerView.setAdapter(mRecyclerAdapter);
+                refreshView(1);
             }
         });
+
+        setButtonStatus(0);
+        refreshView(0);
     }
 
     void setButtonStatus(int index) {
@@ -90,18 +70,35 @@ public class ContactsPager extends BasePager {
         mContactButton.setTextColor(index == 1 ? selectColor : unselectColor);
     }
 
+    void refreshView(int index) {
+        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(mContext, index == 0 ? 1: 2);
+        mRecyclerView.setHasFixedSize(true);
+        mRecyclerView.setLayoutManager(mLayoutManager);
+
+        ContactsRecyclerAdapter adapter = new ContactsRecyclerAdapter(mContext, index == 0 ? 0 : 1, index == 0 ? mRecentList : mContactsList);
+        adapter.setListOnClickListener(mListener);
+        mRecyclerView.setAdapter(adapter);
+    }
+
     public void setDataList(List<ContactModel> mRecentList, List<ContactModel> mContactsList) {
+        mIsSetData = true;
         this.mRecentList = mRecentList;
         this.mContactsList = mContactsList;
 
-        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(mContext, 1);
-        mRecyclerView.setHasFixedSize(true);
-        mRecyclerView.setLayoutManager(mLayoutManager);
+        setButtonStatus(0);
+        refreshView(0);
+    }
 
-        mRecyclerAdapter = new ContactsRecyclerAdapter(mContext, 0, mRecentList);
-        mRecyclerView.setAdapter(mRecyclerAdapter);
+    public void setRecentList(List<ContactModel> recentList) {
+        this.mRecentList = recentList;
+        setButtonStatus(0);
+        refreshView(0);
+    }
 
-        mIsSetData = true;
+    public void setContactsList(List<ContactModel> contactsList) {
+        this.mContactsList = contactsList;
+        setButtonStatus(1);
+        refreshView(1);
     }
 
     public boolean getIsSetData() {