浏览代码

Special battery features

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 年之前
父节点
当前提交
437063e924

+ 1 - 0
app/build.gradle

@@ -125,6 +125,7 @@ dependencies {
 
     implementation 'com.evernote:android-job:1.2.4'
 
+    implementation "com.google.android.gms:play-services-gcm:${googleLibraryVersion}"
     implementation "com.google.firebase:firebase-messaging:${googleLibraryVersion}"
 
     implementation 'com.yarolegovich:lovely-dialog:1.1.0'

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

@@ -76,9 +76,5 @@
             </intent-filter>
         </service>
 
-        <service
-            android:name="com.evernote.android.job.gcm.PlatformGcmService"
-            tools:node="remove"/>
-
     </application>
 </manifest>

+ 17 - 3
app/src/main/java/com/nextcloud/talk/application/NextcloudTalkApplication.java

@@ -21,6 +21,7 @@
 package com.nextcloud.talk.application;
 
 import android.content.Context;
+import android.content.Intent;
 import android.os.Build;
 import android.support.multidex.MultiDex;
 import android.support.multidex.MultiDexApplication;
@@ -28,6 +29,7 @@ import android.util.Log;
 
 import com.evernote.android.job.JobManager;
 import com.evernote.android.job.JobRequest;
+import com.google.android.gms.security.ProviderInstaller;
 import com.nextcloud.talk.BuildConfig;
 import com.nextcloud.talk.dagger.modules.BusModule;
 import com.nextcloud.talk.dagger.modules.ContextModule;
@@ -37,6 +39,7 @@ import com.nextcloud.talk.jobs.AccountRemovalJob;
 import com.nextcloud.talk.jobs.CapabilitiesJob;
 import com.nextcloud.talk.jobs.PushRegistrationJob;
 import com.nextcloud.talk.jobs.creator.MagicJobCreator;
+import com.nextcloud.talk.utils.DeviceUtils;
 import com.nextcloud.talk.utils.DisplayUtils;
 import com.nextcloud.talk.utils.database.user.UserModule;
 import com.nextcloud.talk.webrtc.MagicWebRTCUtils;
@@ -55,8 +58,6 @@ import javax.inject.Singleton;
 import autodagger.AutoComponent;
 import autodagger.AutoInjector;
 
-//import com.google.android.gms.security.ProviderInstaller;
-
 @AutoComponent(
         modules = {
                 BusModule.class,
@@ -69,7 +70,7 @@ import autodagger.AutoInjector;
 
 @Singleton
 @AutoInjector(NextcloudTalkApplication.class)
-public class NextcloudTalkApplication extends MultiDexApplication {
+public class NextcloudTalkApplication extends MultiDexApplication implements ProviderInstaller.ProviderInstallListener {
     private static final String TAG = NextcloudTalkApplication.class.getSimpleName();
 
     //region Public variables
@@ -112,6 +113,7 @@ public class NextcloudTalkApplication extends MultiDexApplication {
     @Override
     public void onCreate() {
         super.onCreate();
+        ProviderInstaller.installIfNeededAsync(this, this);
 
         JobManager.create(this).addJobCreator(new MagicJobCreator());
 
@@ -131,6 +133,8 @@ public class NextcloudTalkApplication extends MultiDexApplication {
         componentApplication.inject(this);
         refWatcher = LeakCanary.install(this);
 
+        DeviceUtils.ignoreSpecialBatteryFeatures();
+
         new JobRequest.Builder(PushRegistrationJob.TAG).setUpdateCurrent(true).startNow().build().schedule();
         new JobRequest.Builder(AccountRemovalJob.TAG).setUpdateCurrent(true).startNow().build().schedule();
 
@@ -180,5 +184,15 @@ public class NextcloudTalkApplication extends MultiDexApplication {
         super.attachBaseContext(base);
         MultiDex.install(this);
     }
+
+    @Override
+    public void onProviderInstalled() {
+
+    }
+
+    @Override
+    public void onProviderInstallFailed(int i, Intent intent) {
+
+    }
     //endregion
 }

+ 107 - 0
app/src/main/java/com/nextcloud/talk/utils/DeviceUtils.java

@@ -0,0 +1,107 @@
+/*
+ * Nextcloud Talk application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.talk.utils;
+
+import android.annotation.SuppressLint;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.os.Build;
+import android.util.Log;
+
+import com.nextcloud.talk.application.NextcloudTalkApplication;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+public class DeviceUtils {
+    private static final String TAG = "DeviceUtils";
+
+    public static void ignoreSpecialBatteryFeatures() {
+        if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) {
+            try {
+                @SuppressLint("PrivateApi") Class<?> aClass = Class.forName("android.miui.AppOpsUtils");
+                if (aClass != null) {
+                    Method getApplicationAutoStart = aClass.getDeclaredMethod("getApplicationAutoStart", Context.class, String.class);
+                    Context applicationContext = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
+                    Object result = getApplicationAutoStart.invoke(aClass, applicationContext, applicationContext.getPackageName());
+                    if (result instanceof Integer) {
+                        Integer integerResult = (Integer) result;
+                        if (integerResult == 0) {
+                            Class<ApplicationInfo> clazz = ApplicationInfo.class;
+                            Field[] fields = clazz.getDeclaredFields();
+
+                            for (Field field : fields) {
+                                field.setAccessible(true);
+                                if (field.getName().equals("FLAG_DISABLE_AUTOSTART")) {
+                                    int value = field.getInt(ApplicationInfo.class);
+                                    if (value != 0) {
+                                        field.setInt(ApplicationInfo.class, 0);
+                                        field.setAccessible(false);
+                                    }
+                                    break;
+                                }
+                                field.setAccessible(false);
+                            }
+                        }
+                    }
+                }
+            } catch (ClassNotFoundException e) {
+                Log.e(TAG, "Class not found");
+            } catch (NoSuchMethodException e) {
+                Log.e(TAG, "No such method");
+            } catch (IllegalAccessException e) {
+                Log.e(TAG, "IllegalAccessException");
+            } catch (InvocationTargetException e) {
+                Log.e(TAG, "InvocationTargetException");
+            }
+        } else if (Build.MANUFACTURER.equalsIgnoreCase("huawei")) {
+            try {
+                @SuppressLint("PrivateApi") Class<?> aClass = Class.forName("com.huawei.systemmanager.optimize.process" +
+                        ".ProtectAppControl");
+                if (aClass != null) {
+                    Method isProtected = aClass.getDeclaredMethod("isProtect", String.class);
+                    Context applicationContext = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
+                    Object result = isProtected.invoke(aClass, applicationContext.getApplicationContext().getPackageName());
+                    if (result instanceof Boolean) {
+                        boolean booleanResult = (boolean) result;
+                        if (!booleanResult) {
+                            Method setProtect = aClass.getDeclaredMethod("setProtect", List.class);
+                            List<String> appsList = new ArrayList<>();
+                            appsList.add(applicationContext.getApplicationContext().getPackageName());
+                            setProtect.invoke(aClass, appsList);
+                        }
+                    }
+                }
+            } catch (ClassNotFoundException e) {
+                Log.e(TAG, "Class not found");
+            } catch (NoSuchMethodException e) {
+                Log.e(TAG, "No such method");
+            } catch (IllegalAccessException e) {
+                Log.e(TAG, "IllegalAccessException");
+            } catch (InvocationTargetException e) {
+                Log.e(TAG, "InvocationTargetException");
+            }
+        }
+    }
+}