observer disable dialer
authorTrillGates <williamtrillgates@gmail.com>
Sat, 14 Aug 2021 10:13:14 +0000 (18:13 +0800)
committerTrillGates <williamtrillgates@gmail.com>
Sat, 14 Aug 2021 10:13:14 +0000 (18:13 +0800)
app/src/main/AndroidManifest.xml
app/src/main/java/com/ic/launcher/MainActivity.java
app/src/main/java/com/ic/launcher/presenter/LauncherPresenter.java
app/src/main/java/com/ic/launcher/presenter/MainPagePresenter.java

index 2c581e8..6ed0195 100644 (file)
@@ -41,7 +41,6 @@
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <action android:name="android.intent.action.VIEW" />
-
                 <category android:name="android.intent.category.HOME" />
                 <category android:name="android.intent.category.DEFAULT" />
                 <category android:name="android.intent.category.MONKEY" />
@@ -50,7 +49,8 @@
         </activity>
         <activity
             android:name=".NotificationActivity"
-            android:theme="@style/ICNotification"/>
+            android:launchMode="singleTop"
+            android:theme="@style/ICNotification" />
     </application>
 
 </manifest>
\ No newline at end of file
index a30d9e3..187cae8 100644 (file)
@@ -44,9 +44,11 @@ public class MainActivity extends AppCompatActivity {
         mainPagePresenter.appListValues.observe(this, newList -> {
             mainPageAdapter.updateAppList(newList);
             //数据设置的时候,把页面移动到中间
-            //实现伪无限滑动
-            looperPage.setCurrentItem(mainPageAdapter.getItemCount() / 2,
-                    false);
+            if (looperPage.getCurrentItem() == 0) {
+                //实现伪无限滑动
+                looperPage.setCurrentItem(mainPageAdapter.getItemCount() / 2,
+                        false);
+            }
         });
     }
 
@@ -74,6 +76,7 @@ public class MainActivity extends AppCompatActivity {
     protected void onDestroy() {
         super.onDestroy();
         looperPage.unregisterOnPageChangeCallback(onPageChangeCallback);
+        mainPagePresenter.onClean();
     }
 
     private final ViewPager2.OnPageChangeCallback onPageChangeCallback = new ViewPager2.OnPageChangeCallback() {
index 002b95d..46e3260 100644 (file)
@@ -3,20 +3,30 @@ package com.ic.launcher.presenter;
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
+import android.database.ContentObserver;
+import android.database.Cursor;
 import android.net.Uri;
+import android.text.TextUtils;
 
+import androidx.annotation.Nullable;
 import androidx.lifecycle.MutableLiveData;
 
 import com.ic.iclooger.ICLogger;
+import com.ic.launcher.R;
 import com.ic.launcher.data.AppItem;
 import com.ic.launcher.data.HardCodeAppListRepository;
 import com.ic.launcher.data.IAppListRepository;
 import com.ic.launcher.utils.SystemUtils;
 
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
+import static com.ic.launcher.utils.Constants.SETTINGS;
+
 /**
  * 首页的业务类
  */
@@ -36,19 +46,73 @@ public class LauncherPresenter extends BasePresenter {
         appListRepository = HardCodeAppListRepository.getInstance(context);
         //去获取应用列表内容
         initAppList();
+        //观察是否要禁止掉拨号
+        observerDisableList();
+    }
+
+    /**
+     * 目前有相机和拨号有可能禁止,禁止的话就干掉吧。
+     */
+    private void observerDisableList() {
+        mContext.getContentResolver().registerContentObserver(Uri.parse(SETTINGS + ".is_dial_enable"),
+                true, dialerObserver);
+    }
+
+    private final ContentObserver dialerObserver = new ContentObserver(null) {
+        @Override
+        public void onChange(boolean selfChange, @Nullable Uri uri) {
+            getDialerEnableState();
+        }
+    };
+
+    private void getDialerEnableState() {
+        executorService.execute(() -> {
+            updateDialerEnableState();
+            initAppList();
+        });
     }
 
+    private final Map<String, String> disableAppList = new HashMap<>();
+
+    /**
+     * 子线程中执行
+     */
+    private void updateDialerEnableState() {
+        Cursor query = mContext.getContentResolver().query(Uri.parse(SETTINGS),
+                new String[]{"s_value"}, "s_key=?", new String[]{"is_dial_enable"}, null);
+        if (query != null) {
+            if (query.moveToNext()) {
+                String value = query.getString(0);
+                //1 : 表示设备可以拨号   0:表示设备不可拨号, 用于同步平台设置。
+                if ("1".equals(value)) {
+                    disableAppList.remove(mContext.getString(R.string.app_package_name_phone));
+                } else {
+                    disableAppList.put(mContext.getString(R.string.app_package_name_phone), "0");
+                }
+            }
+            query.close();
+        }
+    }
 
     public void initAppList() {
         executorService.execute(() -> {
+            updateDialerEnableState();
             //获取应用列表
             List<AppItem> appList = appListRepository.getAppList();
+            Iterator<AppItem> iterator = appList.iterator();
+            while (iterator.hasNext()) {
+                AppItem next = iterator.next();
+                String contains = disableAppList.get(mContext.getString(next.getPackageNameResId()));
+                //删除
+                if (!TextUtils.isEmpty(contains)) {
+                    iterator.remove();
+                }
+            }
             updateDbForAi(appList);
             appListValues.postValue(appList);
         });
     }
 
-
     //添加规则
     public static final String AUTHORITY = "com.ic.ai.info";
     public static final String PATH = "apps";
@@ -85,4 +149,10 @@ public class LauncherPresenter extends BasePresenter {
         }
         return sInstance;
     }
+
+    @Override
+    public void onClean() {
+        mContext.getContentResolver().unregisterContentObserver(dialerObserver);
+        sInstance = null;
+    }
 }
index 071dd16..f7f8bdc 100644 (file)
@@ -27,14 +27,11 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
 import static com.ic.launcher.utils.Constants.NOTIFICATIONS;
+import static com.ic.launcher.utils.Constants.SETTINGS;
 
 
 public class MainPagePresenter extends BasePresenter {
 
-    public static final String PATH = "settings";
-    public static final String AUTHORITY = "com.ic.fzd.settings";
-    public static final String SETTINGS = "content://" + AUTHORITY + "/" + PATH;
-
 
     private static final DateTimeFormatter HH_MM = DateTimeFormatter.ofPattern("HH:mm");
     private static final DateTimeFormatter MM_DD = DateTimeFormatter.ofPattern("MM-dd");