From c89c015430b53c1d9486500e7d2786c010bd690a Mon Sep 17 00:00:00 2001 From: Steven Schoen Date: Wed, 29 Oct 2014 18:31:27 -0400 Subject: [PATCH] Added "restore default" function, fix for HTC devices --- Emoji Switcher/build.gradle | 4 +- .../emojiswitcher/EmojiSwitcherUtils.java | 147 +++++++++++++----- .../emojiswitcher/SwitcherActivity.java | 41 ++--- .../src/main/res/menu/emojiswitcher.xml | 6 +- .../src/main/res/values/strings.xml | 1 + 5 files changed, 134 insertions(+), 65 deletions(-) diff --git a/Emoji Switcher/build.gradle b/Emoji Switcher/build.gradle index bfa563b..a98a4b3 100644 --- a/Emoji Switcher/build.gradle +++ b/Emoji Switcher/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 19 targetSdkVersion 21 - versionCode 7 - versionName "1.5" + versionCode 8 + versionName "1.6" } compileOptions { diff --git a/Emoji Switcher/src/main/java/com/stevenschoen/emojiswitcher/EmojiSwitcherUtils.java b/Emoji Switcher/src/main/java/com/stevenschoen/emojiswitcher/EmojiSwitcherUtils.java index 373a1af..846af99 100644 --- a/Emoji Switcher/src/main/java/com/stevenschoen/emojiswitcher/EmojiSwitcherUtils.java +++ b/Emoji Switcher/src/main/java/com/stevenschoen/emojiswitcher/EmojiSwitcherUtils.java @@ -1,9 +1,13 @@ package com.stevenschoen.emojiswitcher; import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; import android.content.Context; +import android.content.DialogInterface; import android.content.res.AssetManager; import android.os.AsyncTask; +import android.os.Build; import android.widget.Toast; import com.stericson.RootTools.RootTools; @@ -16,6 +20,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Locale; import java.util.concurrent.TimeoutException; public class EmojiSwitcherUtils { @@ -30,13 +35,38 @@ public class EmojiSwitcherUtils { private InstallEmojiSetTask currentInstallTask; - private static String systemEmojiSetFileName = "NotoColorEmoji.ttf"; + private static final String systemFontsPath = "/system/fonts/"; + private static final String systemEmojiFilePath = systemFontsPath + "NotoColorEmoji.ttf"; + private static final String htcFilePath = systemFontsPath + "NotoColorEmoji-htc.ttf"; + private static final String htcBackupFilePath = htcFilePath + ".bak"; + + public static String systemEmojiBackupFilePath(Context context) { + return context.getFilesDir() + File.separator + "backup.ttf"; + } public static boolean isRootReady() { return (RootTools.isRootAvailable() && RootTools.isAccessGiven()); } + public static boolean isHtc() { + return Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).equals("htc"); + } + + public static void applyHtcFix() { + RootTools.copyFile(htcFilePath, htcBackupFilePath, true, true); + RootTools.deleteFileOrDirectory(htcFilePath, false); + } + + public static void undoHtcFix() { + RootTools.copyFile(htcBackupFilePath, htcFilePath, true, true); + RootTools.deleteFileOrDirectory(htcBackupFilePath, false); + } + public void installEmojiSet(Context context, EmojiSet emojiSet) { + if (isHtc()) { + applyHtcFix(); + } + if (currentInstallTask != null) { currentInstallTask.cancel(true); currentInstallTask = null; @@ -47,15 +77,67 @@ public void installEmojiSet(Context context, EmojiSet emojiSet) { } } + public static void applyPermissions(final Activity activity, String permissions, String path) { + Shell shell; + try { + shell = RootTools.getShell(true); + CommandCapture commandPermission = new CommandCapture(0, "chmod " + permissions + " " + path); + shell.add(commandPermission); + } catch (TimeoutException e) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + Toast.makeText(activity, "Error: Timeout", Toast.LENGTH_LONG).show(); + } + }); + e.printStackTrace(); + } catch (RootDeniedException e) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + Toast.makeText(activity, "Error: Root denied", Toast.LENGTH_LONG).show(); + } + }); + e.printStackTrace(); + } catch (IOException e) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + Toast.makeText(activity, "Error: IOException", Toast.LENGTH_LONG).show(); + } + }); + e.printStackTrace(); + } + } + + public static Dialog makeRebootDialog(Context context) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle("Reboot now?"); + builder.setMessage("Most apps require a reboot for new emojis to be recognized."); + builder.setPositiveButton("Reboot", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + RootTools.restartAndroid(); + } + }); + builder.setNegativeButton("No", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + + return builder.create(); + } + private static class InstallEmojiSetTask extends AsyncTask { @Override protected Void doInBackground(Object... params) { final Activity activity = (Activity) params[0]; EmojiSet emojiSet = (EmojiSet) params[1]; - File filesDir = activity.getFilesDir(); - File systemEmojiSetFile = new File("/system/fonts/" + systemEmojiSetFileName); - File backupFile = new File(filesDir + File.separator + "backup.ttf"); + File systemEmojiSetFile = new File(systemEmojiFilePath); + File backupFile = new File(systemEmojiBackupFilePath(activity)); if (backupFile.length() == 0) { RootTools.copyFile(systemEmojiSetFile.getAbsolutePath(), backupFile.getAbsolutePath(), true, false); @@ -64,36 +146,7 @@ protected Void doInBackground(Object... params) { File emojiSetFile = emojiSet.getPath(); RootTools.copyFile(emojiSetFile.getAbsolutePath(), systemEmojiSetFile.getAbsolutePath(), true, false); - Shell shell; - try { - shell = RootTools.getShell(true); - CommandCapture commandPermission = new CommandCapture(0, "chmod 644 " + "/system/fonts/" + systemEmojiSetFileName); - shell.add(commandPermission); - } catch (TimeoutException e) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(activity, "Error: Timeout", Toast.LENGTH_LONG).show(); - } - }); - e.printStackTrace(); - } catch (RootDeniedException e) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(activity, "Error: Root denied", Toast.LENGTH_LONG).show(); - } - }); - e.printStackTrace(); - } catch (IOException e) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(activity, "Error: IOException", Toast.LENGTH_LONG).show(); - } - }); - e.printStackTrace(); - } + applyPermissions(activity, "644", systemEmojiFilePath); return null; } @@ -150,7 +203,8 @@ protected EmojiSet doInBackground(Context... params) { Context context = params[0]; File emojiSetDestinationFile = new File(context.getFilesDir() + File.separator + "systemcurrent.ttf"); - RootTools.copyFile("/system/fonts/" + systemEmojiSetFileName, emojiSetDestinationFile.getAbsolutePath(), true, true); + RootTools.copyFile(systemEmojiFilePath, emojiSetDestinationFile.getAbsolutePath(), true, false); + applyPermissions((Activity) context, "777", emojiSetDestinationFile.getAbsolutePath()); EmojiSet emojiSet = new EmojiSet(emojiSetDestinationFile); try { @@ -180,4 +234,27 @@ protected EmojiSet doInBackground(EmojiSet... params) { return null; } } + + public static class RestoreSystemEmojiTask extends AsyncTask { + private Activity activity; + + @Override + protected Void doInBackground(Activity... activity) { + this.activity = activity[0]; + + if (isHtc()) { + undoHtcFix(); + } + + RootTools.copyFile(systemEmojiBackupFilePath(activity[0]), systemEmojiFilePath, true, true); + applyPermissions(activity[0], "644", systemEmojiFilePath); + + return null; + } + + @Override + protected void onPostExecute(Void nothing) { + makeRebootDialog(activity).show(); + } + } } \ No newline at end of file diff --git a/Emoji Switcher/src/main/java/com/stevenschoen/emojiswitcher/SwitcherActivity.java b/Emoji Switcher/src/main/java/com/stevenschoen/emojiswitcher/SwitcherActivity.java index 0585547..4ee68c0 100644 --- a/Emoji Switcher/src/main/java/com/stevenschoen/emojiswitcher/SwitcherActivity.java +++ b/Emoji Switcher/src/main/java/com/stevenschoen/emojiswitcher/SwitcherActivity.java @@ -1,9 +1,7 @@ package com.stevenschoen.emojiswitcher; import android.app.Activity; -import android.app.AlertDialog; import android.app.ProgressDialog; -import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; @@ -72,7 +70,7 @@ private void init() { buttonRefreshEmojiState.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - fetchCurrentSystemEmojiSet(); + refreshCurrentSystemEmojiSet(); } }); @@ -94,34 +92,19 @@ public void onClick(View v) { @Override public void onClick(View v) { emojiSwitcherUtils.installEmojiSet(SwitcherActivity.this, (EmojiSet) spinnerInstallEmojis.getSelectedItem()); - fetchCurrentSystemEmojiSet(); - AlertDialog.Builder builder = new AlertDialog.Builder(SwitcherActivity.this); - builder.setTitle("Reboot now?"); - builder.setMessage("Most apps require a reboot for new emojis to be recognized."); - builder.setPositiveButton("Reboot", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - RootTools.restartAndroid(); - } - }); - builder.setNegativeButton("No", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.cancel(); - } - }); - builder.show(); + refreshCurrentSystemEmojiSet(); + emojiSwitcherUtils.makeRebootDialog(SwitcherActivity.this).show(); } }); copyEmojiSetsToData(); - fetchCurrentSystemEmojiSet(); + refreshCurrentSystemEmojiSet(); setupBilling(); } - private void fetchCurrentSystemEmojiSet() { + private void refreshCurrentSystemEmojiSet() { verifyRoot(); new EmojiSwitcherUtils.GetCurrentEmojiSetTask() { @@ -315,9 +298,19 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); - if (id == R.id.action_settings) { - return true; + switch (id) { + case R.id.action_restore: + new EmojiSwitcherUtils.RestoreSystemEmojiTask() { + @Override + protected void onPostExecute(Void nothing) { + super.onPostExecute(nothing); + + refreshCurrentSystemEmojiSet(); + } + }.execute(this); + return true; } + return super.onOptionsItemSelected(item); } diff --git a/Emoji Switcher/src/main/res/menu/emojiswitcher.xml b/Emoji Switcher/src/main/res/menu/emojiswitcher.xml index 216e7f4..93b8bb8 100644 --- a/Emoji Switcher/src/main/res/menu/emojiswitcher.xml +++ b/Emoji Switcher/src/main/res/menu/emojiswitcher.xml @@ -3,9 +3,7 @@ tools:context="com.stevenschoen.emojiswitcher.SwitcherActivity"> + android:title="@string/restore_default" /> diff --git a/Emoji Switcher/src/main/res/values/strings.xml b/Emoji Switcher/src/main/res/values/strings.xml index 6134c31..e1aea28 100644 --- a/Emoji Switcher/src/main/res/values/strings.xml +++ b/Emoji Switcher/src/main/res/values/strings.xml @@ -16,5 +16,6 @@ Set emojis to Fonts provided by gonsa from XDA. App by Steven Schoen. Reboot + Restore default \ No newline at end of file