OSDN Git Service

Add activity for external applications to apply icon packs
authorBraden Farmer <farmerbb@gmail.com>
Thu, 29 Sep 2016 18:02:43 +0000 (12:02 -0600)
committerBraden Farmer <farmerbb@gmail.com>
Thu, 29 Sep 2016 18:02:43 +0000 (12:02 -0600)
app/src/main/AndroidManifest.xml
app/src/main/java/com/farmerbb/taskbar/activity/IconPackApplyActivity.java [new file with mode: 0644]
app/src/main/res/values-ja/strings.xml
app/src/main/res/values/strings.xml

index a16c421..9e7d0e0 100644 (file)
         <activity
             android:name=".activity.IconPackActivityDark"
             android:theme="@style/AppTheme.Dialog.Blacklist.Dark"/>
+        <activity
+            android:name=".activity.IconPackApplyActivity"
+            android:theme="@style/AppTheme.Dialog"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN"/>
+                <action android:name="com.farmerbb.taskbar.APPLY_ICON_PACK"/>
+
+                <category android:name="android.intent.category.DEFAULT"/>
+            </intent-filter>
+        </activity>
 
         <service android:name=".service.TaskbarService"/>
         <service android:name=".service.StartMenuService"/>
diff --git a/app/src/main/java/com/farmerbb/taskbar/activity/IconPackApplyActivity.java b/app/src/main/java/com/farmerbb/taskbar/activity/IconPackApplyActivity.java
new file mode 100644 (file)
index 0000000..97ae352
--- /dev/null
@@ -0,0 +1,112 @@
+/* Copyright 2016 Braden Farmer
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.farmerbb.taskbar.activity;
+
+import android.annotation.SuppressLint;
+import android.app.Activity;
+import android.app.ActivityManager;
+import android.app.AlertDialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
+import android.os.Bundle;
+
+import com.farmerbb.taskbar.R;
+import com.farmerbb.taskbar.service.NotificationService;
+import com.farmerbb.taskbar.service.StartMenuService;
+import com.farmerbb.taskbar.service.TaskbarService;
+import com.farmerbb.taskbar.util.U;
+
+public class IconPackApplyActivity extends Activity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        if(getIntent().hasExtra(Intent.EXTRA_PACKAGE_NAME)) {
+            @SuppressLint("InlinedApi")
+            final String iconPackPackage = getIntent().getStringExtra(Intent.EXTRA_PACKAGE_NAME);
+            PackageManager pm = getPackageManager();
+
+            AlertDialog.Builder builder = new AlertDialog.Builder(this);
+            builder.setTitle(R.string.apply_icon_pack)
+                    .setMessage(getString(R.string.apply_icon_pack_description,
+                            pm.getLaunchIntentForPackage(iconPackPackage).resolveActivityInfo(pm, 0).loadLabel(pm)))
+                    .setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            finish();
+                        }
+                    })
+                    .setPositiveButton(R.string.action_ok, new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            SharedPreferences pref = U.getSharedPreferences(IconPackApplyActivity.this);
+                            pref.edit().putString("icon_pack", iconPackPackage).apply();
+
+                            U.refreshPinnedIcons(IconPackApplyActivity.this);
+                            restartTaskbar();
+
+                            finish();
+                        }
+                    });
+
+            AlertDialog dialog = builder.create();
+            dialog.show();
+            dialog.setCancelable(false);
+        } else {
+            U.showToast(this, R.string.must_specify_extra);
+            finish();
+        }
+    }
+
+    private void startTaskbarService(boolean fullRestart) {
+        startService(new Intent(this, TaskbarService.class));
+        startService(new Intent(this, StartMenuService.class));
+        if(fullRestart) startService(new Intent(this, NotificationService.class));
+    }
+
+    private void stopTaskbarService(boolean fullRestart) {
+        stopService(new Intent(this, TaskbarService.class));
+        stopService(new Intent(this, StartMenuService.class));
+        if(fullRestart) stopService(new Intent(this, NotificationService.class));
+    }
+
+    private void restartTaskbar() {
+        SharedPreferences pref = U.getSharedPreferences(this);
+        if(pref.getBoolean("taskbar_active", false) && !pref.getBoolean("is_hidden", false)) {
+            pref.edit().putBoolean("is_restarting", true).apply();
+
+            stopTaskbarService(true);
+            startTaskbarService(true);
+        } else if(isServiceRunning()) {
+            stopTaskbarService(false);
+            startTaskbarService(false);
+        }
+    }
+
+    private boolean isServiceRunning() {
+        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
+        for(ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
+            if(StartMenuService.class.getName().equals(service.service.getClassName()))
+                return true;
+        }
+
+        return false;
+    }
+}
index 4547fe2..a0b4bdd 100644 (file)
     <string name="icon_pack_none">None</string>
     <string name="no_icon_packs_installed">No icon packs installed</string>
 
+    <string name="apply_icon_pack">Apply icon pack</string>
+    <string name="apply_icon_pack_description">Apply \'\'%1$s\'\' to Taskbar?</string>
+    <string name="must_specify_extra">Must specify Intent.EXTRA_PACKAGE_NAME</string>
+
 </resources>
index 764ed03..35a40e7 100644 (file)
     <string name="icon_pack_none">None</string>
     <string name="no_icon_packs_installed">No icon packs installed</string>
 
+    <string name="apply_icon_pack">Apply icon pack</string>
+    <string name="apply_icon_pack_description">Apply \'\'%1$s\'\' to Taskbar?</string>
+    <string name="must_specify_extra">Must specify Intent.EXTRA_PACKAGE_NAME</string>
+
 </resources>