4

I have a rooted Android device and want to replace the softkeys with an own overlay. This works with the app "full!screen" but I want to do this in my own app. I searched and found some questions here but there were no answers to them.

I figured out how to do it with the shell but it is very slow, "full!screen" has a much better performance.

My service with the shell command looks like this:

public class MainService extends Service {
ButtonView mView;
@Override
public void onCreate() {
    super.onCreate();   
    final Bitmap BckBtn = BitmapFactory.decodeResource(getResources(),
            R.drawable.back);


    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            BckBtn.getWidth(), 
            BckBtn.getHeight(),
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            |WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
             PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.LEFT | Gravity.BOTTOM;
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

    mView = new ButtonView(this,BckBtn);
    mView.setOnTouchListener(new OnTouchListener() {


        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            if(arg1.getX()<BckBtn.getWidth() & arg1.getY()>0)
            {
                Process process = null;
                try {
                    process = Runtime.getRuntime().exec("su");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                OutputStream outputStream = process.getOutputStream();
                String cmd = "input keyevent 4";
                try {
                    outputStream.write((cmd + "\n").getBytes("ASCII"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return false;
        }
         });

    wm.addView(mView, params);

    }



@Override
public IBinder onBind(Intent arg0) {
    return null;
}

}



@SuppressLint("DrawAllocation")
class ButtonView extends ViewGroup {

Bitmap BckBtn;

public ButtonView(Context context,Bitmap BckBtn) {
    super(context);

    this.BckBtn=BckBtn;

}


protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(BckBtn,0 , 0, null); 
}


protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}

public boolean onTouchEvent(MotionEvent event) {
    return true;
}
}
PdXY
  • 136
  • 1
  • 2
  • 12
  • 1
    I modified the Service to extend AccessibilityService and use the method _performGlobalAction(1)_ and it is very performant now. On a rooted device I could also enable my Service programmatically by modifying settings.db with the following SQLsentence: `INSERT INTO Secure (accessibility_enabled, enabled_accessibility_services )VALUES ('1', '/' );` (see [here](http://wiki.gpii.net/w/Android)) – PdXY Oct 08 '14 at 14:18
  • did you find a method to simulate the back button other than this? – Visuddha Karunaratne Feb 08 '15 at 12:17
  • performGlobalAction requires api > 15 – Xar-e-ahmer Khan Nov 13 '15 at 06:36

0 Answers0