Android: Slightly rework logging to logcat by offloading duplicated code to a new...
[kugel-rb.git] / android / src / org / rockbox / RockboxFramebuffer.java
blob66e5991a3e9b5b1157031a48c6a761b6c3cbdd31
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Thomas Martitz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 package org.rockbox;
24 import java.nio.ByteBuffer;
26 import android.content.Context;
27 import android.graphics.Bitmap;
28 import android.graphics.Canvas;
29 import android.graphics.Rect;
30 import android.util.DisplayMetrics;
31 import android.view.KeyEvent;
32 import android.view.MotionEvent;
33 import android.view.SurfaceHolder;
34 import android.view.SurfaceView;
35 import android.view.ViewConfiguration;
37 public class RockboxFramebuffer extends SurfaceView
38 implements SurfaceHolder.Callback
40 private final DisplayMetrics metrics;
41 private final ViewConfiguration view_config;
42 private ByteBuffer native_buf;
43 private Bitmap btm;
45 /* first stage init; needs to run from a thread that has a Looper
46 * setup stuff that needs a Context */
47 public RockboxFramebuffer(Context c)
49 super(c);
51 metrics = c.getResources().getDisplayMetrics();
52 view_config = ViewConfiguration.get(c);
53 getHolder().addCallback(this);
54 /* Needed so we can catch KeyEvents */
55 setFocusable(true);
56 setFocusableInTouchMode(true);
57 setClickable(true);
58 /* don't draw until native is ready (2nd stage) */
59 setEnabled(false);
62 /* second stage init; called from Rockbox with information about the
63 * display framebuffer */
64 private void java_lcd_init(int lcd_width, int lcd_height, ByteBuffer native_fb)
66 btm = Bitmap.createBitmap(lcd_width, lcd_height, Bitmap.Config.RGB_565);
67 native_buf = native_fb;
68 setEnabled(true);
71 private void java_lcd_update()
73 SurfaceHolder holder = getHolder();
74 Canvas c = holder.lockCanvas(null);
75 btm.copyPixelsFromBuffer(native_buf);
76 synchronized (holder)
77 { /* draw */
78 c.drawBitmap(btm, 0.0f, 0.0f, null);
80 holder.unlockCanvasAndPost(c);
83 private void java_lcd_update_rect(int x, int y, int width, int height)
85 SurfaceHolder holder = getHolder();
86 Rect dirty = new Rect(x, y, x+width, y+height);
87 Canvas c = holder.lockCanvas(dirty);
88 /* can't copy a partial buffer,
89 * but it doesn't make a noticeable difference anyway */
90 btm.copyPixelsFromBuffer(native_buf);
91 synchronized (holder)
92 { /* draw */
93 c.drawBitmap(btm, dirty, dirty, null);
95 holder.unlockCanvasAndPost(c);
98 public boolean onTouchEvent(MotionEvent me)
100 int x = (int) me.getX();
101 int y = (int) me.getY();
103 switch (me.getAction())
105 case MotionEvent.ACTION_CANCEL:
106 case MotionEvent.ACTION_UP:
107 touchHandler(false, x, y);
108 return true;
109 case MotionEvent.ACTION_MOVE:
110 case MotionEvent.ACTION_DOWN:
111 touchHandler(true, x, y);
112 return true;
115 return false;
118 public boolean onKeyDown(int keyCode, KeyEvent event)
120 return buttonHandler(keyCode, true);
123 public boolean onKeyUp(int keyCode, KeyEvent event)
125 return buttonHandler(keyCode, false);
128 private int getDpi()
130 return metrics.densityDpi;
134 private int getScrollThreshold()
136 return view_config.getScaledTouchSlop();
139 private native void touchHandler(boolean down, int x, int y);
140 public native static boolean buttonHandler(int keycode, boolean state);
142 public native void surfaceCreated(SurfaceHolder holder);
143 public native void surfaceDestroyed(SurfaceHolder holder);
144 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)