Android 4.1.2系统中,通过 设置->声音->触摸时震动 选项可以禁止触摸震动,但无法禁止解锁震动和关机震动,比较烦,于是研究了一番,修改framework和services就可以让该设置同样对解锁和关机震动有效。
首先修改frameworks/base/services/java/com/android/server/pm/ShutdownThread.java以关联 触摸时震动 和关机震动,关机时会调用
rebootOrShutdown函数,我们可以看到在该函数中,如果SHUTDOWN_VIBRATE_MS大于0,则创建一个SystemVibrator,执行震动效果,所以我们在此处修改即可:
-
/**
-
* Do not call this directly. Use {@link #reboot(Context, String, boolean)}
-
* or {@link #shutdown(Context, boolean)} instead.
-
*
-
* @param reboot true to reboot or false to shutdown
-
* @param reason reason for reboot
-
*/
-
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);
-
}
-
} else if (SHUTDOWN_VIBRATE_MS > 0) {
-
boolean vibrateEnabled = Settings.System.getInt(sInstance.mContext.getContentResolver(),
-
Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 1;
-
if (vibrateEnabled) {
-
// vibrate before shutting down
-
Vibrator vibrator = new SystemVibrator();
-
try {
-
vibrator.vibrate(SHUTDOWN_VIBRATE_MS);
-
} catch (Exception e) {
-
// Failure to vibrate shouldn't interrupt shutdown. Just log it.
-
Log.w(TAG, "Failed to vibrate during shutdown.", e);
-
}
-
-
// vibrator is asynchronous so we need to wait to avoid shutting down too soon.
-
try {
-
Thread.sleep(SHUTDOWN_VIBRATE_MS);
-
} catch (InterruptedException unused) {
-
}
-
}
-
}
-
-
// Shutdown power
-
Log.i(TAG, "Performing low-level shutdown...");
-
PowerManagerService.lowLevelShutdown();
-
}
对于解锁震动,则需要修改frameworks/base/core/java/com/android/internal/widget/multiwaveview/GlowPadView.java文件,我们可以看到GlowPadView类有一个setVibrateEnabled函数,即是设置该控件是否震动,而解锁界面则使用了该控件,所以我们只需在该类初始化的时候检测是否设置了触摸时震动选项,而后设置该控件是否可以震动即可:
-
public GlowPadView(Context context, AttributeSet attrs) {
-
super(context, attrs);
-
Resources res = context.getResources();
-
-
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GlowPadView);
-
mInnerRadius = a.getDimension(R.styleable.GlowPadView_innerRadius, mInnerRadius);
-
mOuterRadius = a.getDimension(R.styleable.GlowPadView_outerRadius, mOuterRadius);
-
mSnapMargin = a.getDimension(R.styleable.GlowPadView_snapMargin, mSnapMargin);
-
mVibrationDuration = a.getInt(R.styleable.GlowPadView_vibrationDuration,
-
mVibrationDuration);
-
mFeedbackCount = a.getInt(R.styleable.GlowPadView_feedbackCount,
-
mFeedbackCount);
-
mHandleDrawable = new TargetDrawable(res,
-
a.peekValue(R.styleable.GlowPadView_handleDrawable).resourceId);
-
mHandleDrawable.setState(TargetDrawable.STATE_INACTIVE);
-
mOuterRing = new TargetDrawable(res,
-
getResourceId(a, R.styleable.GlowPadView_outerRingDrawable));
-
-
mAlwaysTrackFinger = a.getBoolean(R.styleable.GlowPadView_alwaysTrackFinger, false);
-
-
int pointId = getResourceId(a, R.styleable.GlowPadView_pointDrawable);
-
Drawable pointDrawable = pointId != 0 ? res.getDrawable(pointId) : null;
-
mGlowRadius = a.getDimension(R.styleable.GlowPadView_glowRadius, 0.0f);
-
-
TypedValue outValue = new TypedValue();
-
-
// Read array of target drawables
-
if (a.getValue(R.styleable.GlowPadView_targetDrawables, outValue)) {
-
internalSetTargetResources(outValue.resourceId);
-
}
-
if (mTargetDrawables == null || mTargetDrawables.size() == 0) {
-
throw new IllegalStateException("Must specify at least one target drawable");
-
}
-
-
// Read array of target descriptions
-
if (a.getValue(R.styleable.GlowPadView_targetDescriptions, outValue)) {
-
final int resourceId = outValue.resourceId;
-
if (resourceId == 0) {
-
throw new IllegalStateException("Must specify target descriptions");
-
}
-
setTargetDescriptionsResourceId(resourceId);
-
}
-
-
// Read array of direction descriptions
-
if (a.getValue(R.styleable.GlowPadView_directionDescriptions, outValue)) {
-
final int resourceId = outValue.resourceId;
-
if (resourceId == 0) {
-
throw new IllegalStateException("Must specify direction descriptions");
-
}
-
setDirectionDescriptionsResourceId(resourceId);
-
}
-
-
a.recycle();
-
-
// Use gravity attribute from LinearLayout
-
a = context.obtainStyledAttributes(attrs, android.R.styleable.LinearLayout);
-
mGravity = a.getInt(android.R.styleable.LinearLayout_gravity, Gravity.TOP);
-
a.recycle();
-
-
boolean vibrateEnabled = Settings.System.getInt(context.getContentResolver(),
-
Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) == 1;
-
setVibrateEnabled(vibrateEnabled ? mVibrationDuration > 0 : false);
-
-
assignDefaultsIfNeeded();
-
-
mPointCloud = new PointCloud(pointDrawable);
-
mPointCloud.makePointCloud(mInnerRadius, mOuterRadius);
-
mPointCloud.glowManager.setRadius(mGlowRadius);
-
}
通过以上两处改动,即可实现 触摸时震动 选项对解锁震动和关机震动同样生效。修改后编译,将生成的framework.jar,framework2.jar和services.jar放到/system/framework目录下,并修改其权限为644,重启即可生效。
阅读(10755) | 评论(1) | 转发(1) |