[Android Q&A, Help & Troubleshooting] Tom04:
Hello there!

I would need some help reverse engineering an app and recompiling it with some different code afterwards.

The App coordinates all the different components of my phone to work properly.

When you press the two fingerprint sensors, the app gives you haptic feedback and turns the screen from front to back or vice versa, depending on which side is facing up.

I need the app to do exactly that once when i wake up my phone either with fingerprint or power button, so the display illuminates which is facing up, and not the one i used recently.

I have some experience with java, but all i got are the .smali documents.

Is there any way to reliably get smali into java and back?

Code:

package org.mokee.settings.keyhandler;

import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.os.Vibrator;
import android.provider.Settings.Secure;
import android.provider.Settings.System;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import com.android.internal.os.DeviceKeyHandler;
import org.mokee.internal.util.FileUtils;
import org.mokee.internal.util.PowerMenuConstants;
import org.mokee.settings.detector.MotionDetector;
import org.mokee.settings.detector.MotionDetector.MotionListener;
import org.mokee.settings.dualscreen.ScreenHelper;

public class KeyHandler implements DeviceKeyHandler {
private static final long DEBOUNCE_DELAY_MILLIS = 150;
public static final String FINGER_SWITCH_SETTING_KEY = "finger_switch_switch";
public static final String FORCE_FRONT_ON_CALL = "force_front_on_call";
public static final int FP_LEFT_KEY = 133;
public static final int FP_RIGHT_KEY = 134;
private static final int SWITCH_WAKELOCK_DURATION = 3000;
public static final String WAKE_FRONT_ON_CALL = "wake_front_on_call";
/* access modifiers changed from: private */
public static boolean sForceFrontOnRinging = true;
/* access modifiers changed from: private */
public static boolean sInCallRinging = false;
/* access modifiers changed from: private */
public static boolean sPressSwitch = true;
/* access modifiers changed from: private */
public static boolean sScreenTurnedOn = true;
/* access modifiers changed from: private */
public static boolean sWakeFrontOnRinging = true;
private KeyguardManager keyguardManager;
/* access modifiers changed from: private */
public final Context mContext;
private long mLeftFpDownTime = 0;
private boolean mLeftKeyPressed = false;
/* access modifiers changed from: private */
public MotionDetector mMotionDetector;
private final PowerManager mPowerManager;
private long mRightFpDownTime = 0;
private boolean mRightKeyPressed = false;
private final BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.SCREEN_OFF")) {
KeyHandler.sScreenTurnedOn = false;
} else if (intent.getAction().equals("android.intent.action.SCREEN_ON")) {
KeyHandler.sScreenTurnedOn = true;
if (KeyHandler.sInCallRinging && KeyHandler.sWakeFrontOnRinging) {
KeyHandler.this.mSwitchScreenHandler.post(new SwitchRunnable(1));
}
} else if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
if (KeyHandler.this.mTelephonyManager == null) {
KeyHandler.this.mTelephonyManager = (TelephonyManager) KeyHandler.this.mContext.getSystemService("phone");
}
if (KeyHandler.this.mTelephonyManager.getCallState() == 1) {
KeyHandler.sInCallRinging = true;
if (KeyHandler.sScreenTurnedOn && KeyHandler.sForceFrontOnRinging) {
KeyHandler.this.mSwitchScreenHandler.post(new SwitchRunnable(1));
return;
}
return;
}
KeyHandler.sInCallRinging = false;
}
}
};
/* access modifiers changed from: private */
public ScreenHelper mSwitchHelper;
/* access modifiers changed from: private */
public Handler mSwitchScreenHandler;
private WakeLock mSwitchWakeLock;
/* access modifiers changed from: private */
public TelephonyManager mTelephonyManager;
private final Vibrator mVibrator;

class SettingsObserver extends ContentObserver {
public SettingsObserver(Handler handler) {
super(handler);
}

/* access modifiers changed from: 0000 */
public void observe() {
ContentResolver contentResolver = KeyHandler.this.mContext.getContentResolver();
contentResolver.registerContentObserver(System.getUriFor(KeyHandler.WAKE_FRONT_ON_CALL), false, this, -1);
contentResolver.registerContentObserver(System.getUriFor(KeyHandler.FORCE_FRONT_ON_CALL), false, this, -1);
contentResolver.registerContentObserver(System.getUriFor(KeyHandler.FINGER_SWITCH_SETTING_KEY), false, this, -1);
update();
}

public void onChange(boolean z) {
update();
}

/* access modifiers changed from: 0000 */
public void update() {
ContentResolver contentResolver = KeyHandler.this.mContext.getContentResolver();
boolean z = true;
KeyHandler.sPressSwitch = System.getInt(contentResolver, KeyHandler.FINGER_SWITCH_SETTING_KEY, 1) == 1;
KeyHandler.sWakeFrontOnRinging = System.getInt(contentResolver, KeyHandler.WAKE_FRONT_ON_CALL, 1) == 1;
if (System.getInt(contentResolver, KeyHandler.FORCE_FRONT_ON_CALL, 1) != 1) {
z = false;
}
KeyHandler.sForceFrontOnRinging = z;
}
}

private class SwitchRunnable implements Runnable {
private long mSwitchCompleteTime = 0;
private int mTarget;

public SwitchRunnable(int i) {
this.mTarget = i;
}

public void run() {
if (this.mTarget != ScreenHelper.getHelper().getCurrentDisplayId()) {
doSwitch();
}
}

private void doSwitch() {
if (this.mSwitchCompleteTime > 0 && SystemClock.uptimeMillis() - this.mSwitchCompleteTime < 50) {
try {
Thread.sleep(Math.abs(SystemClock.uptimeMillis() - this.mSwitchCompleteTime));
} catch (Exception e) {
}
}
KeyHandler.this.mSwitchHelper.setCurrentDisplay(this.mTarget);
readLcdState(this.mTarget);
KeyHandler.this.mMotionDetector.setCurrentDisplayId(this.mTarget);
FileUtils.writeLine("/sys/class/backlight/panel0-backlight/brightness", FileUtils.readOneLine("/sys/class/backlight/panel0-backlight/brightness"));
this.mSwitchCompleteTime = SystemClock.uptimeMillis();
}

private void readLcdState(int i) {
int i2 = 0;
while (i2 < 200) {
int parseInt = Integer.parseInt(FileUtils.readOneLine("/sys/kernel/lcd_enhance/lcd_state"));
if (i == parseInt || parseInt > 1) {
i2++;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
return;
}
}
}
}

public KeyHandler(Context context) {
this.mContext = context;
this.mVibrator = (Vibrator) context.getSystemService(Vibrator.class);
this.mPowerManager = (PowerManager) context.getSystemService(PowerMenuConstants.GLOBAL_ACTION_KEY_POWER);
this.mSwitchWakeLock = this.mPowerManager.newWakeLock(1, "NubiaPartsGestureWakeLock:");
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.SCREEN_OFF");
intentFilter.addAction("android.intent.action.SCREEN_ON");
intentFilter.addAction("android.intent.action.PHONE_STATE");
context.registerReceiver(this.mScreenStateReceiver, intentFilter);
this.mSwitchHelper = ScreenHelper.getHelper();
this.mSwitchScreenHandler = new Handler(this.mSwitchHelper.getLooper());
initMotionDetector();
initSettingsObserver();
}

private void initSettingsObserver() {
new SettingsObserver(new Handler()).observe();
}

private void initMotionDetector() {
this.mMotionDetector = new MotionDetector(this.mContext);
this.mMotionDetector.setMotionListener(new MotionListener() {
public void onMotionChange(int i) {
if (KeyHandler.sScreenTurnedOn) {
KeyHandler.this.mSwitchScreenHandler.post(new SwitchRunnable(i));
}
}
});
this.mMotionDetector.setCurrentDisplayId(ScreenHelper.getHelper().getCurrentDisplayId());
}

public KeyEvent handleKeyEvent(KeyEvent keyEvent) {
if (!hasSetupCompleted()) {
return keyEvent;
}
if (this.keyguardManager == null) {
this.keyguardManager = (KeyguardManager) this.mContext.getSystemService(KeyguardManager.class);
}
if (this.keyguardManager.isKeyguardLocked() || !sScreenTurnedOn) {
return keyEvent;
}
int keyCode = keyEvent.getKeyCode();
if (keyCode != 133 && keyCode != 134) {
return keyEvent;
}
boolean z = true;
if (keyEvent.getAction() != 1) {
z = false;
}
if (z) {
return interceptFpKeyUp(keyEvent);
}
return interceptFpKeyDown(keyEvent);
}

private KeyEvent interceptFpKeyDown(KeyEvent keyEvent) {
switch (keyEvent.getKeyCode()) {
case 133:
if (!this.mLeftKeyPressed && (keyEvent.getFlags() & 1024) == 0) {
this.mLeftKeyPressed = true;
this.mLeftFpDownTime = keyEvent.getDownTime();
triggerDoubleFpAction();
break;
}
case 134:
if (!this.mRightKeyPressed && (keyEvent.getFlags() & 1024) == 0) {
this.mRightKeyPressed = true;
this.mRightFpDownTime = keyEvent.getDownTime();
triggerDoubleFpAction();
break;
}
}
return null;
}

private KeyEvent interceptFpKeyUp(KeyEvent keyEvent) {
switch (keyEvent.getKeyCode()) {
case 133:
this.mLeftKeyPressed = false;
this.mLeftFpDownTime = 0;
break;
case 134:
this.mRightKeyPressed = false;
this.mRightFpDownTime = 0;
break;
}
if (this.mMotionDetector.isEnable()) {
this.mMotionDetector.disable();
}
return null;
}

private void triggerDoubleFpAction() {
if (this.mLeftKeyPressed && this.mRightKeyPressed && sPressSwitch) {
long uptimeMillis = SystemClock.uptimeMillis();
if (uptimeMillis <= this.mLeftFpDownTime + DEBOUNCE_DELAY_MILLIS && uptimeMillis <= this.mRightFpDownTime + DEBOUNCE_DELAY_MILLIS) {
this.mSwitchWakeLock.acquire(3000);
doHapticFeedback();
this.mMotionDetector.enable();
}
}
}

private void doHapticFeedback() {
if (this.mVibrator != null && this.mVibrator.hasVibrator()) {
this.mVibrator.vibrate(50);
}
}

private boolean hasSetupCompleted() {
return Secure.getInt(this.mContext.getContentResolver(), "user_setup_complete", 0) != 0;
}
}

Sorry for my bad english!

Thanks!