Skip to content

Commit

Permalink
Added "restore default" function, fix for HTC devices
Browse files Browse the repository at this point in the history
  • Loading branch information
DSteve595 committed Oct 29, 2014
1 parent daae2a2 commit c89c015
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 65 deletions.
4 changes: 2 additions & 2 deletions Emoji Switcher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 21
versionCode 7
versionName "1.5"
versionCode 8
versionName "1.6"
}

compileOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand All @@ -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<Object, Void, Void> {
@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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -180,4 +234,27 @@ protected EmojiSet doInBackground(EmojiSet... params) {
return null;
}
}

public static class RestoreSystemEmojiTask extends AsyncTask<Activity, Void, Void> {
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -72,7 +70,7 @@ private void init() {
buttonRefreshEmojiState.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fetchCurrentSystemEmojiSet();
refreshCurrentSystemEmojiSet();
}
});

Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 2 additions & 4 deletions Emoji Switcher/src/main/res/menu/emojiswitcher.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
tools:context="com.stevenschoen.emojiswitcher.SwitcherActivity">

<item
android:id="@+id/action_settings"
android:visible="false"
android:orderInCategory="100"
android:id="@+id/action_restore"
android:showAsAction="never"
android:title="@string/action_settings" />
android:title="@string/restore_default" />
</menu>
1 change: 1 addition & 0 deletions Emoji Switcher/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
<string name="set_emojis_to_">Set emojis to</string>
<string name="about">Fonts provided by gonsa from XDA. App by Steven Schoen.</string>
<string name="reboot">Reboot</string>
<string name="restore_default">Restore default</string>

</resources>

0 comments on commit c89c015

Please sign in to comment.