編輯:關於Android編程
1,設置靜音和振動
靜音和振動都屬於來電後的動作.所以在設置靜音和振動時都只是設置一些標識,並往數據庫寫入相應標識.
文件:packages/apps/settings/src/com/android/settings/SoundAndDisplaySettings.java
[java]
<SPAN style="COLOR: #993300">private CheckBoxPreference mSilent;
private CheckBoxPreference mVibrate;
private void setRingerMode(boolean silent, boolean vibrate) {
if (silent) {
mAudioManager.setRingerMode(vibrate ? AudioManager.RINGER_MODE_VIBRATE :
AudioManager.RINGER_MODE_SILENT);
} else {
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
vibrate ? AudioManager.VIBRATE_SETTING_ON
: AudioManager.VIBRATE_SETTING_OFF);
}
}
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference == mSilent || preference == mVibrate) {
setRingerMode(mSilent.isChecked(), mVibrate.isChecked());
if (preference == mSilent) updateState(false);
}</SPAN>
private CheckBoxPreference mSilent;
private CheckBoxPreference mVibrate;
private void setRingerMode(boolean silent, boolean vibrate) {
if (silent) {
mAudioManager.setRingerMode(vibrate ? AudioManager.RINGER_MODE_VIBRATE :
AudioManager.RINGER_MODE_SILENT);
} else {
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
vibrate ? AudioManager.VIBRATE_SETTING_ON
: AudioManager.VIBRATE_SETTING_OFF);
}
}
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference == mSilent || preference == mVibrate) {
setRingerMode(mSilent.isChecked(), mVibrate.isChecked());
if (preference == mSilent) updateState(false);
}
靜音和振動是復選框按鈕,兩個中有一個發生變化時調用setRingerMode對狀態進行設置;如下狀態描術:
RINGER_MODE_SILENT 靜音,且無振動
RINGER_MODE_VIBRATE 靜音,但有振動
RINGER_MODE_NORMAL 正常聲音,振動開關由setVibrateSetting決定.
鈴響模式的設置是通過mAudioManager(音頻管理器)來實現的.
2 音頻管理器服務
mAudioManager所在服務如下:
文件: frameworks/base/media/java/android/media/AudioManager.java
[java]
<SPAN style="COLOR: #993300"> public static final int RINGER_MODE_SILENT = 0;
public static final int RINGER_MODE_VIBRATE = 1;
public static final int RINGER_MODE_NORMAL = 2;
public void setRingerMode(int ringerMode) {
IAudioService service = getService();
try {
service.setRingerMode(ringerMode);
} catch (RemoteException e) {
Log.e(TAG, "Dead object in setRingerMode", e);
}
}</SPAN>
public static final int RINGER_MODE_SILENT = 0;
public static final int RINGER_MODE_VIBRATE = 1;
public static final int RINGER_MODE_NORMAL = 2;
public void setRingerMode(int ringerMode) {
IAudioService service = getService();
try {
service.setRingerMode(ringerMode);
} catch (RemoteException e) {
Log.e(TAG, "Dead object in setRingerMode", e);
}
}
將鈴響模式值傳給音頻接口服務IaudioService[java] view plaincopyprint?
<SPAN style="COLOR: #993300">public static final int VIBRATE_TYPE_RINGER = 0;
public static final int VIBRATE_TYPE_NOTIFICATION = 1;
public static final int VIBRATE_SETTING_OFF = 0;
public static final int VIBRATE_SETTING_ON = 1;
public static final int VIBRATE_SETTING_ONLY_SILENT = 2;
public void setVibrateSetting(int vibrateTyp , int vibrateSetting) {
IAudioService service = getService();
try {
service.setVibrateSetting(vibrateType, vibrateSetting);
} catch (RemoteException e) {
Log.e(TAG, "Dead object in setVibrateSetting", e);
}
}</SPAN>
public static final int VIBRATE_TYPE_RINGER = 0;
public static final int VIBRATE_TYPE_NOTIFICATION = 1;
public static final int VIBRATE_SETTING_OFF = 0;
public static final int VIBRATE_SETTING_ON = 1;
public static final int VIBRATE_SETTING_ONLY_SILENT = 2;
public void setVibrateSetting(int vibrateTyp , int vibrateSetting) {
IAudioService service = getService();
try {
service.setVibrateSetting(vibrateType, vibrateSetting);
} catch (RemoteException e) {
Log.e(TAG, "Dead object in setVibrateSetting", e);
}
}
將振動類型和振動設置傳給音頻接口服務IaudioService,IaudioService的定義如下:
frameworks/base/media/java/android/media/IAudioService.aidl
frameworks/base/media/java/android/media/AudioService.java
文件: frameworks/base/media/java/android/media/AudioService.java
文件: frameworks/base/core/java/android/provider/Settings.java
[java]
<SPAN style="COLOR: #993300">public void setRingerMode(int ringerMode) {
synchronized (mSettingsLock) {
if (ringerMode != mRingerMode) {
setRingerModeInt(ringerMode, true);
// Send sticky broadcast
broadcastRingerMode();
}
}
}</SPAN>
public void setRingerMode(int ringerMode) {
synchronized (mSettingsLock) {
if (ringerMode != mRingerMode) {
setRingerModeInt(ringerMode, true);
// Send sticky broadcast
broadcastRingerMode();
}
}
}將對應模式下的音量寫入數據庫,並將該模式廣播.[java] view plaincopyprint?
<SPAN style="COLOR: #993300">public void setVibrateSetting(int vibrateType, int vibrateSetting) {
mVibrateSetting = getValueForVibrateSetting(mVibrateSetting, vibrateType, vibrateSetting);
// Broadcast change
broadcastVibrateSetting(vibrateType);
// Post message to set ringer mode (it in turn will post a message
// to persist)
sendMsg(mAudioHandler, MSG_PERSIST_VIBRATE_SETTING, SHARED_MSG, SENDMSG_NOOP, 0, 0,
null, 0);
}</SPAN>
public void setVibrateSetting(int vibrateType, int vibrateSetting) {
mVibrateSetting = getValueForVibrateSetting(mVibrateSetting, vibrateType, vibrateSetting);
// Broadcast change
broadcastVibrateSetting(vibrateType);
// Post message to set ringer mode (it in turn will post a message
// to persist)
sendMsg(mAudioHandler, MSG_PERSIST_VIBRATE_SETTING, SHARED_MSG, SENDMSG_NOOP, 0, 0,
null, 0);
}
同樣將振動模式寫入數據庫,並廣播該模式.
3 硬件服務
文件:frameworks/base/services/java/com/android/server/HardwareService.java
開始振動:
[java]
<SPAN style="COLOR: #993300">public void vibrate(long milliseconds, IBinder token) {
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Requires VIBRATE permission");
}
// We're running in the system server so we cannot crash. Check for a
// timeout of 0 or negative. This will ensure that a vibration has
// either a timeout of > 0 or a non-null pattern.
if (milliseconds <= 0 || (mCurrentVibration != null
&& mCurrentVibration.hasLongerTimeout(milliseconds))) {
// Ignore this vibration since the current vibration will play for
// longer than milliseconds.
return;
}
Vibration vib = new Vibration(token, milliseconds);
synchronized (mVibrations) {
removeVibrationLocked(token);
doCancelVibrateLocked();
mCurrentVibration = vib;
startVibrationLocked(vib);
}
}
private void startVibrationLocked(final Vibration vib) {
if (vib.mTimeout != 0) {
vibratorOn(vib.mTimeout);
mH.postDelayed(mVibrationRunnable, vib.mTimeout);
} else {
// mThread better be null here. doCancelVibrate should always be
// called before startNextVibrationLocked or startVibrationLocked.
mThread = new VibrateThread(vib);
mThread.start();
}
}</SPAN>
public void vibrate(long milliseconds, IBinder token) {
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Requires VIBRATE permission");
}
// We're running in the system server so we cannot crash. Check for a
// timeout of 0 or negative. This will ensure that a vibration has
// either a timeout of > 0 or a non-null pattern.
if (milliseconds <= 0 || (mCurrentVibration != null
&& mCurrentVibration.hasLongerTimeout(milliseconds))) {
// Ignore this vibration since the current vibration will play for
// longer than milliseconds.
return;
}
Vibration vib = new Vibration(token, milliseconds);
synchronized (mVibrations) {
removeVibrationLocked(token);
doCancelVibrateLocked();
mCurrentVibration = vib;
startVibrationLocked(vib);
}
}
private void startVibrationLocked(final Vibration vib) {
if (vib.mTimeout != 0) {
vibratorOn(vib.mTimeout);
mH.postDelayed(mVibrationRunnable, vib.mTimeout);
} else {
// mThread better be null here. doCancelVibrate should always be
// called before startNextVibrationLocked or startVibrationLocked.
mThread = new VibrateThread(vib);
mThread.start();
}
}
該接口允許設置振動的時間長度,通過調用vibratorOn(vib.mTimeout);實現對底層硬件的操作。
取消振動[java]
<SPAN style="COLOR: #993300">public void cancelVibrate(IBinder token) {
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.VIBRATE,
"cancelVibrate");
// so wakelock calls will succeed
long identity = Binder.clearCallingIdentity();
try {
synchronized (mVibrations) {
final Vibration vib = removeVibrationLocked(token);
if (vib == mCurrentVibration) {
doCancelVibrateLocked();
startNextVibrationLocked();
}
}
}
finally {
Binder.restoreCallingIdentity(identity);
}
}
private void doCancelVibrateLocked() {
if (mThread != null) {
synchronized (mThread) {
mThread.mDone = true;
mThread.notify();
}
mThread = null;
}
vibratorOff ();
mH.removeCallbacks(mVibrationRunnable);
}</SPAN>
public void cancelVibrate(IBinder token) {
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.VIBRATE,
"cancelVibrate");
// so wakelock calls will succeed
long identity = Binder.clearCallingIdentity();
try {
synchronized (mVibrations) {
final Vibration vib = removeVibrationLocked(token);
if (vib == mCurrentVibration) {
doCancelVibrateLocked();
startNextVibrationLocked();
}
}
}
finally {
Binder.restoreCallingIdentity(identity);
}
}
private void doCancelVibrateLocked() {
if (mThread != null) {
synchronized (mThread) {
mThread.mDone = true;
mThread.notify();
}
mThread = null;
}
vibratorOff ();
mH.removeCallbacks(mVibrationRunnable);
}
該接口允許停止振動,通過調用vibratorOff();實現對底層硬件的操作。
4 硬件調用
vibratorOn、vibratorOff對應的jni代碼如下:
[java]
<SPAN style="COLOR: #993300">static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms)
{
// LOGI("vibratorOn/n");
vibrator_on(timeout_ms);
}
static void vibratorOff(JNIEnv *env, jobject clazz)
{
// LOGI("vibratorOff/n");
vibrator_off();
}</SPAN>
static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms)
{
// LOGI("vibratorOn/n");
vibrator_on(timeout_ms);
}
static void vibratorOff(JNIEnv *env, jobject clazz)
{
// LOGI("vibratorOff/n");
vibrator_off();
}
vibrator_on、vibrator_off 接口的提供者為如下硬件原型。
5, 硬件原型
文件:hardware/libhardware_legacy/vibrator/vibrator.c
#define THE_DEVICE "/sys/class/timed_output/vibrator/enable"
[java]
<SPAN style="COLOR: #993300">static int sendit(int timeout_ms)
{
int nwr, ret, fd;
char value[20];
#ifdef QEMU_HARDWARE
if (qemu_check()) {
return qemu_control_command( "vibrator:%d", timeout_ms );
}
#endif
fd = open(THE_DEVICE, O_RDWR);
if(fd < 0)
return errno;
nwr = sprintf(value, "%d/n", timeout_ms);
ret = write(fd, value, nwr);
close(fd);
return (ret == nwr) ? 0 : -1;
}
int vibrator_on(int timeout_ms)
{
/* constant on, up to maximum allowed time */
return sendit(timeout_ms);
}
int vibrator_off()
{
return sendit(0);
}</SPAN>
static int sendit(int timeout_ms)
{
int nwr, ret, fd;
char value[20];
#ifdef QEMU_HARDWARE
if (qemu_check()) {
return qemu_control_command( "vibrator:%d", timeout_ms );
}
#endif
fd = open(THE_DEVICE, O_RDWR);
if(fd < 0)
return errno;
nwr = sprintf(value, "%d/n", timeout_ms);
ret = write(fd, value, nwr);
close(fd);
return (ret == nwr) ? 0 : -1;
}
int vibrator_on(int timeout_ms)
{
/* constant on, up to maximum allowed time */
return sendit(timeout_ms);
}
int vibrator_off()
{
return sendit(0);
}
由以上代碼可知,開啟振動時是往文件/sys/class/timed_output/vibrator/enable寫入振動的時間長度;關閉振動時,其時間長度為0。/sys/class/timed_output/vibrator/enable 的真實路徑根據實際作修改。
6,驅動代碼
創建timed_output類
kernel/drivers/staging/android/Timed_output.c
在sys/class目錄創建timed_output子目錄和文件enable
timed_output_class = class_create(THIS_MODULE, "timed_output");創建timed_output子目錄
ret = device_create_file(tdev->dev, &dev_attr_enable);在sys/class/timed_output子目錄創建文件enable
[java]
<SPAN style="COLOR: #993300">static int create_timed_output_class(void)
{
if (!timed_output_class) {
timed_output_class = class_create(THIS_MODULE, "timed_output");
if (IS_ERR(timed_output_class))
return PTR_ERR(timed_output_class);
atomic_set(&device_count, 0);
}
return 0;
}
int timed_output_dev_register(struct timed_output_dev *tdev)
{
int ret;
if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time)
return -EINVAL;
ret = create_timed_output_class();
if (ret < 0)
return ret;
tdev->index = atomic_inc_return(&device_count);
tdev->dev = device_create(timed_output_class, NULL,
MKDEV(0, tdev->index), NULL, tdev->name);
if (IS_ERR(tdev->dev))
return PTR_ERR(tdev->dev);
ret = device_create_file(tdev->dev, &dev_attr_enable);
if (ret < 0)
goto err_create_file;
dev_set_drvdata(tdev->dev, tdev);
tdev->state = 0;
return 0;
err_create_file:
device_destroy(timed_output_class, MKDEV(0, tdev->index));
printk(KERN_ERR "timed_output: Failed to register driver %s/n",
tdev->name);
return ret;
}
EXPORT_SYMBOL_GPL(timed_output_dev_register);</SPAN>
static int create_timed_output_class(void)
{
if (!timed_output_class) {
timed_output_class = class_create(THIS_MODULE, "timed_output");
if (IS_ERR(timed_output_class))
return PTR_ERR(timed_output_class);
atomic_set(&device_count, 0);
}
return 0;
}
int timed_output_dev_register(struct timed_output_dev *tdev)
{
int ret;
if (!tdev || !tdev->name || !tdev->enable || !tdev->get_time)
return -EINVAL;
ret = create_timed_output_class();
if (ret < 0)
return ret;
tdev->index = atomic_inc_return(&device_count);
tdev->dev = device_create(timed_output_class, NULL,
MKDEV(0, tdev->index), NULL, tdev->name);
if (IS_ERR(tdev->dev))
return PTR_ERR(tdev->dev);
ret = device_create_file(tdev->dev, &dev_attr_enable);
if (ret < 0)
goto err_create_file;
dev_set_drvdata(tdev->dev, tdev);
tdev->state = 0;
return 0;
err_create_file:
device_destroy(timed_output_class, MKDEV(0, tdev->index));
printk(KERN_ERR "timed_output: Failed to register driver %s/n",
tdev->name);
return ret;
}
EXPORT_SYMBOL_GPL(timed_output_dev_register);
驅動注冊馬達的驅動,注冊一個定時器用於控制震動時間(回調函數vibrator_timer_func),注冊兩個隊列,一共給馬達打開用,一共為馬達震動關閉用。[java] view plaincopyprint?
<SPAN style="COLOR: #993300">static void pmic_vibrator_on(struct work_struct *work)
{
set_pmic_vibrator(1);
}
static void pmic_vibrator_off(struct work_struct *work)
{
set_pmic_vibrator(0);
}
static void timed_vibrator_on(struct timed_output_dev *sdev)
{
schedule_work(&work_vibrator_on);
}
static void timed_vibrator_off(struct timed_output_dev *sdev)
{
schedule_work(&work_vibrator_off);
}
static void vibrator_enable(struct timed_output_dev *dev, int value)
{
hrtimer_cancel(&vibe_timer);
if (value == 0)
timed_vibrator_off(dev);
else {
value = (value > 15000 ? 15000 : value);
timed_vibrator_on(dev);
hrtimer_start(&vibe_timer,
ktime_set(value / 1000, (value % 1000) * 1000000),
HRTIMER_MODE_REL);
}
}
static int vibrator_get_time(struct timed_output_dev *dev)
{
if (hrtimer_active(&vibe_timer)) {
ktime_t r = hrtimer_get_remaining(&vibe_timer);
return r.tv.sec * 1000 + r.tv.nsec / 1000000;
} else
return 0;
}
static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
{
timed_vibrator_off(NULL);
return HRTIMER_NORESTART;
}
static struct timed_output_dev pmic_vibrator = {
.name = "vibrator",
.get_time = vibrator_get_time,
.enable = vibrator_enable,
};
void __init pxa_init_pmic_vibrator(void)
{
INIT_WORK(&work_vibrator_on, pmic_vibrator_on);
INIT_WORK(&work_vibrator_off, pmic_vibrator_off);
hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
vibe_timer.function = vibrator_timer_func;
timed_output_dev_register(&pmic_vibrator);
}
當上層要設置馬達震動時,往文件/sys/class/timed_output/vibrator/enable寫入振動的時間長度,通過
static ssize_t enable_store(
struct device *dev, struct device_attribute *attr,
const char *buf, size_t size)
{
struct timed_output_dev *tdev = dev_get_drvdata(dev);
int value;
sscanf(buf, "%d", &value);
tdev->enable(tdev, value);
return size;
}</SPAN>
static void pmic_vibrator_on(struct work_struct *work)
{
set_pmic_vibrator(1);
}
static void pmic_vibrator_off(struct work_struct *work)
{
set_pmic_vibrator(0);
}
static void timed_vibrator_on(struct timed_output_dev *sdev)
{
schedule_work(&work_vibrator_on);
}
static void timed_vibrator_off(struct timed_output_dev *sdev)
{
schedule_work(&work_vibrator_off);
}
static void vibrator_enable(struct timed_output_dev *dev, int value)
{
hrtimer_cancel(&vibe_timer);
if (value == 0)
timed_vibrator_off(dev);
else {
value = (value > 15000 ? 15000 : value);
timed_vibrator_on(dev);
hrtimer_start(&vibe_timer,
ktime_set(value / 1000, (value % 1000) * 1000000),
HRTIMER_MODE_REL);
}
}
static int vibrator_get_time(struct timed_output_dev *dev)
{
if (hrtimer_active(&vibe_timer)) {
ktime_t r = hrtimer_get_remaining(&vibe_timer);
return r.tv.sec * 1000 + r.tv.nsec / 1000000;
} else
return 0;
}
static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
{
timed_vibrator_off(NULL);
return HRTIMER_NORESTART;
}
static struct timed_output_dev pmic_vibrator = {
.name = "vibrator",
.get_time = vibrator_get_time,
.enable = vibrator_enable,
};
void __init pxa_init_pmic_vibrator(void)
{
INIT_WORK(&work_vibrator_on, pmic_vibrator_on);
INIT_WORK(&work_vibrator_off, pmic_vibrator_off);
hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
vibe_timer.function = vibrator_timer_func;
timed_output_dev_register(&pmic_vibrator);
}
當上層要設置馬達震動時,往文件/sys/class/timed_output/vibrator/enable寫入振動的時間長度,通過
static ssize_t enable_store(
struct device *dev, struct device_attribute *attr,
const char *buf, size_t size)
{
struct timed_output_dev *tdev = dev_get_drvdata(dev);
int value;
sscanf(buf, "%d", &value);
tdev->enable(tdev, value);
return size;
}
調用驅動的enable函數也就是vibrator_enable( .enable = vibrator_enable,)
vibrator_enable
|
|
v
timed_vibrator_on(dev);
|
|
v
schedule_work(&work_vibrator_on);
|
|
v
pmic_vibrator_on
|
|
v
set_pmic_vibrator(1); //給馬達供電震動
|
|
v
hrtimer_start(&vibe_timer,
ktime_set(value / 1000, (value % 1000) * 1000000),
HRTIMER_MODE_REL);
最終是設置馬達的硬件控制驅動管給馬達供電,並且啟動定時器,定時時間是上層給的參數。
定時時間到了就調用定時器的回調函數vibrator_timer_func
vibrator_timer_func
|
|
v
timed_vibrator_off(NULL);
|
|
v
schedule_work(&work_vibrator_off);
|
|
v
pmic_vibrator_off
|
|
v
set_pmic_vibrator(0); //斷開馬達的供電,馬達停止震動
最終是設置馬達的硬件控制驅動管斷開馬達供電,停止馬達震動
我這裡只是簡單的用了兩個listview來實現的,先上效果圖。比較粗糙。預留了自定義的空間。思路:從上圖應該可以看的出來。就是上下兩個listview。點擊下面的ltem
(1)布局文件layout (2)需要彈出的Toast布局文件 (3)類的文件 package com
背景項目目前存在使用久了或者重復打開關閉某個頁面,內存會一直飙升,居高不下,頻繁發生GC。靜置一段時間後,情況有所改善,但是問題依舊明顯,如圖1-1、1-2。圖1-1.操
前兩天寫的上章關於換膚的功能獲得了很好的反響,今天為大家介紹另一種方式。今天實現的策略也是網友建議的,然後我自己去寫了個demo,大家自己評估下相比第一種方式的優勢和劣