2012년 10월 18일 목요일

Navigation Bar에 volume up/down, Shutdown, Screenshot 버튼 넣어보기





Tablet UI에서 back, home. recent app 옆에  volume up/down, Shutdown, Screenshot button을 넣어보자.

먼저 frameworks/base/packages/SystemUI/res/drawable-[h,m,x]dpi/에
ic_sysbar_home.png와 동일한 크기의 
ic_sysbar_plus,png, ic_sysbar_minus.png, ic_sysbar_shutdown.png, ic_sysbar_screenshot.png 이란 이미지를 준비합니다.

frameworks/base/packages/SystemUI/res/layout/navigation_bar.xml 에 recent_apps 다음에 4개의 항목을 추가합니다.

 <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/recent_apps"
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_recent"
                android:layout_weight="0"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:contentDescription="@string/accessibility_recent"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/vol_plus"
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_plus"
                android:layout_weight="0"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:contentDescription="@string/accessibility_recent"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/vol_minus"
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_minus"
                android:layout_weight="0"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:contentDescription="@string/accessibility_recent"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/shutdown"
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_shutdown"
                android:layout_weight="0"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:contentDescription="@string/accessibility_recent"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/screenshot"
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_screenshot"
                android:layout_weight="0"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:contentDescription="@string/accessibility_recent"
                />

frameworks/base/packages/SystemUI/res/layout-sw600dp/status_bar.xml도 동일하게 수정 합니다.

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java 에서 volume up/down과 shutdown을 구현합니다.


//volume control
import android.media.AudioManager;
//shutdown
import com.android.internal.app.ShutdownThread;
//screenshot
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Messenger;


    ImageView mBackButton;
    View mHomeButton;
    View mMenuButton;
    View mRecentButton;
//버튼 3개 추가
    View mVolPlusButton;
    View mVolMinusButton;
    View mShutdownButton;
    View mScreenshotButton;

    ViewGroup mFeedbackIconArea; // notification icons, IME icon, compat icon
    InputMethodButton mInputMethodSwitchButton;
    CompatModeButton mCompatModeButton;





    // used to notify status bar for suppressing notification LED
    private boolean mPanelSlightlyVisible;

AudioManager mAudioManager;

    public Context getContext() { return mContext; }






        mMenuButton = mNavigationArea.findViewById(R.id.menu);
        mRecentButton = mNavigationArea.findViewById(R.id.recent_apps);
        mRecentButton.setOnClickListener(mOnClickListener);
mVolPlusButton = mNavigationArea.findViewById(R.id.vol_plus);
        mVolPlusButton.setOnClickListener(mOnClickListener);
mVolMinusButton = mNavigationArea.findViewById(R.id.vol_minus);
        mVolMinusButton.setOnClickListener(mOnClickListener);
mShutdownButton = mNavigationArea.findViewById(R.id.shutdown);
        mShutdownButton.setOnClickListener(mOnClickListener);

mScreenshotButton = mNavigationArea.findViewById(R.id.screenshot);
        mScreenshotButton.setOnClickListener(mOnClickListener);

        LayoutTransition lt = new LayoutTransition();











        mHomeButton.setVisibility(disableHome ? View.INVISIBLE : View.VISIBLE);
        mRecentButton.setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE);
        mVolPlusButton.setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE);
        mVolMinusButton.setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE);
        mShutdownButton.setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE);
        mScreenshotButton.setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE);

        mInputMethodSwitchButton.setScreenLocked(
                (visibility & StatusBarManager.DISABLE_SYSTEM_INFO) != 0);













    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
if (mAudioManager == null)
mAudioManager = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
            if (v == mRecentButton) {
                onClickRecentButton();
            } else if (v == mInputMethodSwitchButton) {
                onClickInputMethodSwitchButton();
            } else if (v == mCompatModeButton) {
                onClickCompatModeButton();
            } else if (v == mVolPlusButton) {
mAudioManager.handleKeyDown(KeyEvent.KEYCODE_VOLUME_DOWN, AudioManager.STREAM_MUSIC);
} else if (v == mVolMinusButton) {
mAudioManager.handleKeyDown(KeyEvent.KEYCODE_VOLUME_UP, AudioManager.STREAM_MUSIC);
} else if (v == mShutdownButton) {
ShutdownThread.shutdown(mContext,true);
}

} else if (v == mScreenshotButton) {
takeScreenshot(); 
}
        }
    };

final Object mScreenshotLock = new Object();
    ServiceConnection mScreenshotConnection = null;

    final Runnable mScreenshotTimeout = new Runnable() {
        @Override public void run() {
            synchronized (mScreenshotLock) {
                if (mScreenshotConnection != null) {
                    mContext.unbindService(mScreenshotConnection);
                    mScreenshotConnection = null;
                }
            }
        }
    };

    // Assume this is called from the Handler thread.
    private void takeScreenshot() {
        synchronized (mScreenshotLock) {
            if (mScreenshotConnection != null) {
                return;
            }
            ComponentName cn = new ComponentName("com.android.systemui",
                    "com.android.systemui.screenshot.TakeScreenshotService");
            Intent intent = new Intent();
            intent.setComponent(cn);
            ServiceConnection conn = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    synchronized (mScreenshotLock) {
                        if (mScreenshotConnection != this) {
                            return;
                        }
                        Messenger messenger = new Messenger(service);
                        Message msg = Message.obtain(null, 1);
                        final ServiceConnection myConn = this;
                        Handler h = new Handler(mHandler.getLooper()) {
                            @Override
                            public void handleMessage(Message msg) {
                                synchronized (mScreenshotLock) {
                                    if (mScreenshotConnection == myConn) {
                                        mContext.unbindService(mScreenshotConnection);
                                        mScreenshotConnection = null;
                                        mHandler.removeCallbacks(mScreenshotTimeout);
                                    }
                                }
                            }
                        };
                        msg.replyTo = new Messenger(h);
                        msg.arg1 = msg.arg2 = 0;
                        //if (mStatusBar != null && mStatusBar.isVisibleLw())
                            msg.arg1 = 1;
                        //if (mNavigationBar != null && mNavigationBar.isVisibleLw())
                            msg.arg2 = 1;
                        try {
                            messenger.send(msg);
                        } catch (RemoteException e) {
                        }
                    }
                }
                @Override
                public void onServiceDisconnected(ComponentName name) {}
            };
            if (mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
                mScreenshotConnection = conn;
                mHandler.postDelayed(mScreenshotTimeout, 10000);
            }
        }

    }

2012년 10월 17일 수요일

자동으로 Wi-Fi on/off하기

무제한 요금제를 사용하지만 Wi-Fi의 환경이 더 빠르기 때문에 사무실이나 집에서 전원을 연결하고 Wi-Fi를 사용합니다.
그리고 이동 중에는 Wi-Fi를 off합니다.
그래서 자동으로 충전 중이면 Wi-Fi를 켜주고 충전이 아니면 Wi-Fi를 끄는 app을 만들어 봤습니다.

추가적으로 빈 Activity에 사용여부 확인 가능한 checkbox를 추가하였습니다.


http://dl.dropbox.com/u/4485660/WiFiAuto.apk

아주 간단한 프로그램인데 BroadcastReceiver 상속받은 class만으로 app을 만들어 보니 ACTION_POWER_CONNECT intent를 못 받아서 삽질 좀 했네요.
그래서 아무 동작하지 않는 Activity를 추가 하였습니다.


--- ACConnectReceiver.java ---


package com.hardkernel.odroid.wifiauto;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;

public class ACConnectReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
manager.setWifiEnabled(true);
} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
manager.setWifiEnabled(false);
}
}

}


--- MainActivity.java ---



package com.hardkernel.odroid.wifiauto;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.main_layout);

CheckBox cbUse = (CheckBox)findViewById(R.id.cb_use);
cbUse.setOnCheckedChangeListener(new OnCheckedChangeListener () {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
SharedPreferences prefs = getSharedPreferences("PreName", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("Use", isChecked);
editor.commit();
}

});

SharedPreferences prefs = getSharedPreferences("PreName", MODE_PRIVATE);
cbUse.setChecked(prefs.getBoolean("Use", true));
}

}



--- AndroidManifest.xml ---


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hardkernel.odroid.wifiauto"
    android:versionCode="1"
    android:versionName="1.0"
    android:installLocation="internalOnly">

    <uses-sdk android:minSdkVersion="10" />
 
    <uses-feature android:name="android.hardware.wifi" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.DEVICE_POWER"/>

    <application android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme">
             
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >  
            <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
     
        <receiver
            android:name=".ACConnectReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
            </intent-filter> 
        </receiver>

    </application>

</manifest>