2014년 7월 27일 일요일

[Android] Stop Audio Player

Stop Audio Player 


        Intent intent = new Intent("com.android.music.musicservicecommand");
        intent.putExtra("command", "pause");

        mContext.sendBroadcast(intent);


MediaPlaybackService commands


 public class MediaPlaybackService extends Service {

     public static final String PLAYSTATE_CHANGED = "com.android.music.playstatechanged";
     public static final String META_CHANGED = "com.android.music.metachanged";
     public static final String QUEUE_CHANGED = "com.android.music.queuechanged";

     public static final String SERVICECMD = "com.android.music.musicservicecommand";
     public static final String CMDNAME = "command";
     public static final String CMDTOGGLEPAUSE = "togglepause";
     public static final String CMDSTOP = "stop";
     public static final String CMDPAUSE = "pause";
     public static final String CMDPLAY = "play";
     public static final String CMDPREVIOUS = "previous";
     public static final String CMDNEXT = "next";
     public static final String TOGGLEPAUSE_ACTION = "com.android.music.musicservicecommand.togglepause";
     public static final String PAUSE_ACTION = "com.android.music.musicservicecommand.pause";
     public static final String PREVIOUS_ACTION = "com.android.music.musicservicecommand.previous";
     public static final String NEXT_ACTION = "com.android.music.musicservicecommand.next";

2014년 5월 1일 목요일

[Android] Stop Running Task.

Sleep mode 진입 시 구동중인 Application을 정리(중지)
- Sleep / Wake Up 반복시 Task 쌓이는 현상을 피하기 위해 사용.


1. Stop Running Application

    private void stopExecutedApplication() {
        mRunningTaskInfo = mActivityManager.getRunningTasks(Integer.MAX_VALUE);

        for(int i = 0; i < mRunningTaskInfo.size(); i++) {
            int taskId = mRunningTaskInfo.get(i).id;
            String pkgName = mRunningTaskInfo.get(i).baseActivity.getPackageName();
            mActivityManager.removeTask(taskId, ActivityManager.REMOVE_TASK_KILL_PROCESS);
         }
     }


2014년 4월 30일 수요일

[Android] Global Action Dialog

Android 종료 PopUp

Android 기기의 Power key Long Press에 의해 나타나는 Dialog Flow 정리.
- Power Key 를 500ms 이상 누르고 있을 경우, 종료 팝업 D/P


PhoneWindowManager.java ( frameworks/base/policy/src/com/android/internal/policy/impl )
    private final Runnable mPowerLongPress = new Runnable() {
            case LONG_PRESS_POWER_GLOBAL_ACTIONS:
                mPowerKeyHandled = true;
                if (!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false)) {
                    performAuditoryFeedbackForAccessibilityIfNeed();
                }
                sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
                showGlobalActionsDialog();

    private void interceptPowerKeyDown(boolean handled) {
        mPowerKeyHandled = handled;
        if (!handled) {
            mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
        }
    }

ViewConfiguration.java ( frameworks/base/core/java/android/view/ )
    /**
     * Defines the duration in milliseconds a user needs to hold down the
     * appropriate button to bring up the global actions dialog (power off,
     * lock screen, etc).
     */
    private static final int GLOBAL_ACTIONS_KEY_TIMEOUT = 500;

    /**
     * The amount of time a user needs to press the relevant key to bring up
     * the global actions dialog.
     *
     * @return how long a user needs to press the relevant key to bring up
     *   the global actions dialog.
     */
    public static long getGlobalActionKeyTimeout() {
        return GLOBAL_ACTIONS_KEY_TIMEOUT;
    }



2014년 4월 17일 목요일

[Android] Package명으로 Application 실행하기

Package 명으로 Application Start 시키기

Intent intent =
mContext.getPackageManager().getLaunchIntentForPackage(mAppName);

if (intent == null)
    Log.i(TAG,"startActivity Failed. intent is NULL");
else
    mContext.startActivity(intent);



2014년 2월 20일 목요일

[Android] Programmable Bluetooth On/Off.

Android Device의 Bluetooth On/Off 설정 제어하기.


Permissions : 

And add the following permissions into your manifest file:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

Control On/Off : 

Sample code
public static boolean setBluetooth(boolean enable) {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    boolean isEnabled = bluetoothAdapter.isEnabled();
    if (enable && !isEnabled) {
        return bluetoothAdapter.enable(); 
    }
    else if(!enable && isEnabled) {
        return bluetoothAdapter.disable();
    }
    // No need to change bluetooth state
    return true;
}

But remember these important points:
This is an asynchronous call: it will return immediately, and clients should listen for ACTION_STATE_CHANGED to be notified of subsequent adapter state changes. If this call returns true, then the adapter state will immediately transition from STATE_OFF to STATE_TURNING_ON, and some time later transition to either STATE_OFF or STATE_ON. If this call returns false then there was an immediate problem that will prevent the adapter from being turned on - such as Airplane mode, or the adapter is already turned on.

[Android] Programmable Wi-Fi On/Off

Android Device의 Wi-Fi On/Off 설정 제어하기.


Permissions :

And add the following permissions into your manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

Control On/Off.

WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);

Check Status.

boolean wifiEnabled = wifiManager.isWifiEnabled()


2014년 2월 17일 월요일

Linux Partion Label 확인

Display Linux Partition Label

$ sudo e2label /dev/sda1

Set a new Label

# e2label /dev/sdb2 usbstroage
or
# tune2fs -L usbstroage /dev/sdb2

2014년 2월 15일 토요일

[Android] Adb Shell with RK3066

Rock Chip Device Adb shell 사용하기.

- 참조 : http://linux-rockchip.info/mw/index.php?title=ADB_shell_with_RK3066


Rock Chip device들은 Moborobo 라는 tool을 이용해서 adb 연결을 해야했다.
덕분에 원하지 않더라도 단말에 Moborobo daemon이 설치된다.
개인적으로 이런식의 Apk 설치를 싫어한다.

Windows 와 Linux 환경에서 USB Vendor ID 를 추가를 통해 Android SDK 에 포함된 adb를 이용한 동작을 확인했다.

1. Linux 


1.1 Rock Chip device ( RK3066, RK3188,...) 정보를 Udev rules 파일에 추가.

- Linux 머신과 Device 연결 후 USB Vendor ID 확인. ( RockChip USB Vendor ID는 "2207" 이다.)

$ lsusb
Bus 002 Device 023: ID 2207:0010
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

- Device 정보(USB Vendor ID)를 "/etc/udev/rules.d/51-android.rules" 파일에 추가 후 udev를 restart.
$ sudo vi /etc/udev/rules.d/51-android.rules
SUBSYSTEM=="usb", ATTR{idVendor}=="2207", MODE="0666", GROUP="plugdev"
$ sudo udevadm control --reload-rules

1.2 adb_usb.ini 파일에 RockChip 의 Vendor ID 추가
$ vi ~/.android/adb_usb.ini

# ANDROID 3RD PARTY USB VENDOR ID LIST -- DO NOT EDIT.
# USE 'android update adb' TO GENERATE.
# 1 USB VENDOR ID PER LINE.
0x2207

2. Windows


2.1 Device의 USB Vendor ID를 android_winusb.inf 파일에 추가.
     해당 파일은 Android SDK가 설치된 아래 경로에 존재한다.

path : android\sdk\extras\google\usb_driver\android_winusb.inf
;
; Android WinUsb driver installation.
;


;Google Glass
%SingleAdbInterface%        = USB_Install, USB\VID_18D1&PID_9001
%CompositeAdbInterface%     = USB_Install, USB\VID_18D1&PID_9001&MI_01


;Rock Chip
%SingleAdbInterface%        = USB_Install, USB\VID_2207&PID_0006&M1_01
%CompositeAdbInterface%     = USB_Install, USB\VID_2207&PID_0006&REV_0222&MI_01


2.2 하드웨어 ID 확인하기.
    - 장치관리자의 속성에서 확인 가능






















2.3 adb_usb.ini 파일에 RockChip 의 Vendor ID 추가


$ vi /cygdrive/c/Users/wschoi/.android/adb_usb.ini

# ANDROID 3RD PARTY USB VENDOR ID LIST -- DO NOT EDIT.
# USE 'android update adb' TO GENERATE.
# 1 USB VENDOR ID PER LINE.
0x2207

2.4 Adb 연결 확인










오늘은 여기까지....

2014년 1월 8일 수요일

[Android] Device Power Off Flow


전원키를 통한 안드로이드 디바이스 Power Off의 경우 아래와 같은 flow를 통해 전원 컨트롤이 이루어진다.
PowerManageService ( ShutdownThread :: rebootOrShutdown(boolean reboot, String reason))
-> JNI -> System Call
-> kernel -> PMIC

============================================================
Android Frameworks
============================================================
PowerManagerService.java

    /**
     * Reboots the device.
     */
    public void reboot(boolean confirm, String reason, boolean wait) {
        try {
            shutdownOrRebootInternal(false, confirm, reason, wait);
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }

    /**
     * Shuts down the device.
     */
    @Override // Binder call
    public void shutdown(boolean confirm, boolean wait) {
        try {
            shutdownOrRebootInternal(true, confirm, null, wait);
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }

    private void shutdownOrRebootInternal(final boolean shutdown, final boolean confirm,
            final String reason, boolean wait) {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    if (shutdown) {
                        ShutdownThread.shutdown(mContext, confirm);
                    } else {
                        ShutdownThread.reboot(mContext, reason, confirm);
                    }
                }
            }
        };
         ......
    }


ShutdownThread.java
    public static void rebootOrShutdown(boolean reboot, String reason) {
        if (reboot) {
            Log.i(TAG, "Rebooting, reason: " + reason);
            try {
                PowerManagerService.lowLevelReboot(reason);
            } catch (Exception e) {
                Log.e(TAG, "Reboot failed, will attempt shutdown instead", e);
            }
            ......
        }

        // Shutdown power
        Log.i(TAG, "Performing low-level shutdown...");
        PowerManagerService.lowLevelShutdown();
    }

PowerManagerService.java
    /**
     * Low-level function turn the device off immediately, without trying
     * to be clean.  Most people should use {@link ShutdownThread} for a clean shutdown.
     */
    public static void lowLevelShutdown() {
        nativeShutdown();
    }

============================================================
JNI / Native
============================================================
com_android_server_power_PowerManagerService.cpp
static void nativeShutdown(JNIEnv *env, jclass clazz) {
    android_reboot(ANDROID_RB_POWEROFF, 0, 0);
}

static JNINativeMethod gPowerManagerServiceMethods[] = {
    /* name, signature, funcPtr */
    ......
    { "nativeShutdown", "()V",
            (void*) nativeShutdown },
    ......
};

Android_reboot.c
int android_reboot(int cmd, int flags, char *arg)
{
    switch (cmd) {
        case ANDROID_RB_RESTART:
            ret = reboot(RB_AUTOBOOT);
            break;

        case ANDROID_RB_POWEROFF:
            ret = reboot(RB_POWER_OFF);
            break;

        case ANDROID_RB_RESTART2:
            ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
                           LINUX_REBOOT_CMD_RESTART2, arg);
            break;

        default:
            ret = -1;
    }

reboot.h
#define RB_POWER_OFF    LINUX_REBOOT_CMD_POWER_OFF

============================================================
Kernel
============================================================
kernel/sys.c
/*
 * Reboot system call: for obvious reasons only root may call it,
 * and even root needs to set up some magic numbers in the registers
 * so that some mistake won't make this reboot the whole machine.
 * You can also set the meaning of the ctrl-alt-del-key here.
 *
 * reboot doesn't sync: do that yourself before calling this.
 */
SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
void __user *, arg)
{
    ......
switch (cmd) {
        ......
case LINUX_REBOOT_CMD_HALT:
kernel_halt();
do_exit(0);
panic("cannot halt");

case LINUX_REBOOT_CMD_POWER_OFF:
kernel_power_off();
do_exit(0);
break;
        ......
       }
}