2014年9月27日 星期六

[android] 開啟Debug log

private static final String TAG = "LocationManagerService";
public static final boolean D = Log.isLoggable(TAG, Log.DEBUG);


透過adb 開啟
adb shell setprop log.tag.LocationManagerService DEBUG  

[android] 開啟圖檔

    private Bitmap openPic(String file) {
        if (file == null)
            return null;
        if (DEB_PIC) Log.d(TAG,"openPic() file = " + file);        
        File f = new File(file);
        if (f.exists()) {
            return BitmapFactory.decodeFile(file);
        } else {
            Log.d(TAG,"openPic() "+ file+" not exists");
            return null;
        }    
    }

[android] 轉存png檔

 private void savePic(Bitmap b,String strFileName) {
        //Log.e(TAG, "savePic()"); 
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(strFileName);
            if (null != fos) {
                b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
            }            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

[android] 抓螢幕截圖 java code

private void getPic() {
        Bitmap mScreenBitmap;
        Matrix mDisplayMatrix;
        mDisplayMatrix = new Matrix();  

        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

        if (wm == null) {
            Log.d(TAG,"getPic() wm = null");
            return;
        }
        wm.getDefaultDisplay().getRealMetrics(metrics);              
        float[] dims = {metrics.widthPixels, metrics.heightPixels};

        float degrees = getDegreesForRotation(wm.getDefaultDisplay().getRotation());  
        boolean requiresRotation = (degrees > 0);  
        if (requiresRotation) {  
            // Get the dimensions of the device in its native orientation  
            mDisplayMatrix.reset();  
            mDisplayMatrix.preRotate(-degrees);  
            mDisplayMatrix.mapPoints(dims);  
            dims[0] = Math.abs(dims[0]);  
            dims[1] = Math.abs(dims[1]);  
        }  
 
        mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]); 

        if (mScreenBitmap == null) {
            Log.d(TAG,"getPic() mScreenBitmap = null");
            return;
        }        

        if (requiresRotation) {  
            // Rotate the screenshot to the current orientation  
            Bitmap ss = Bitmap.createBitmap(metrics.widthPixels, metrics.heightPixels, Bitmap.Config.ARGB_8888);  
            Canvas c = new Canvas(ss);  
            c.translate(ss.getWidth() / 2, ss.getHeight() / 2);  
            c.rotate(degrees);  
            c.translate(-dims[0] / 2, -dims[1] / 2);  
            c.drawBitmap(mScreenBitmap, 0, 0, null);  
            c.setBitmap(null);  
            // Recycle the previous bitmap  
            mScreenBitmap.recycle();  
            mScreenBitmap = ss;  
        }  
  
        // Optimizations  
        mScreenBitmap.setHasAlpha(false);  
        mScreenBitmap.prepareToDraw();        
    }

[android] 抓top activity 名稱


private String getTopActivity() {
        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        if (am == null) {
            Log.d(TAG,"Go() am = null");
            return null;
        }
        // get the info from the currently running task
        List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

        //Log.d("topActivity", "CURRENT Activity ::"
        //        + taskInfo.get(0).topActivity.getClassName());

        ComponentName componentInfo = taskInfo.get(0).topActivity;
        //Log.d(TAG, "getPackageName = " + componentInfo.getPackageName());
        return componentInfo.getPackageName();
    }

[android] java code 送touch event

    private void touch(int x, int y) {
        try {
            Runtime.getRuntime().exec("input tap "+x+" "+y);

        } catch (Throwable t) {
        }
    }

[android] service 處理音量鍵

android service 收不到key event.
可以透過 AUDIO_SERVICE

    
public class SettingsContentObserver extends ContentObserver {
        int previousVolume;
        Context context;

        public SettingsContentObserver(Context c, Handler handler) {
            super(handler);
            Log.d(TAG,"SettingsContentObserver()");
            context=c;

            AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            previousVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
        }

        @Override
        public boolean deliverSelfNotifications() {
            Log.d(TAG,"deliverSelfNotifications()");
            return super.deliverSelfNotifications();
        }

        @Override
        public void onChange(boolean selfChange) {
            Log.d(TAG,"onChange()");
            super.onChange(selfChange);

            AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            int currentVolume = audio.getStreamVolume(AudioManager.STREAM_SYSTEM);            
            Log.d(TAG,"onChange() currentVolume = "+currentVolume);
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate()");
        mSettingsContentObserver = new SettingsContentObserver(this,new Handler());
        getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver );        
    }
    @Override
    public void onDestroy() {
        Log.d(TAG,"onDestroy()");
        getContentResolver().unregisterContentObserver(mSettingsContentObserver);
    }

[android] 關於android 手機遊戲外掛

一點小小的想法!
經過實作確定可行!
python + adb command 實作andorid 手機的遊戲外掛

透過python 下adb command 截圖並pull 到電腦端

def getPic():
    os.chdir(adb)
    os.system("adb shell screencap -p /sdcard/screen.png")
    os.system("adb pull /sdcard/screen.png "+pic_dir+"pic.png")
    os.system("adb shell rm /sdcard/screen.png")

接著透過 python PIL lib 做圖形識別
接著對手機送touch event
def touch(x,y):
 os.chdir(adb)
 os.system("adb shell input tap "+str(x)+" "+str(y)) 

優點:
        實作超快! 省時省力
缺點:
        需要開著電腦!

2014年9月26日 星期五

[程式語言] syntaxhighlighter

syntaxhighlighter
可讓程式碼變得更好看
官網: http://alexgorbatchev.com/SyntaxHighlighter/

安裝方式 參考
http://alexgorbatchev.com/SyntaxHighlighter/manual/installation.html


Brush name
Brush aliases
File name
bash, shell
shBrushBash.js
cpp, c
shBrushCpp.js
css
shBrushCss.js
js, jscript, javascript
shBrushJScript.js
java
shBrushJava.js
perl, pl
shBrushPerl.js
php
shBrushPhp.js
py, python
shBrushPython.js
xml, xhtml, xslt, html, xhtml
shBrushXml.js

程式碼用下面語法包起來

<pre class="brush: java">
</pre">


public class Hello {
    public static int gvar;
    public static void say(String s) {
        int x = 10;
        System.out.print(s+x);
    }
    public static void main(String[] argv) {
        float y = 0;
        say("Hello, world\n");
    }
}
<pre class="brush: python">
</pre">

print("Hello, world!")