Chinaunix首页 | 论坛 | 博客
  • 博客访问: 964585
  • 博文数量: 113
  • 博客积分: 7235
  • 博客等级: 少将
  • 技术积分: 2101
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-14 11:24
文章分类

全部博文(113)

文章存档

2013年(7)

2012年(5)

2011年(6)

2010年(8)

2009年(15)

2008年(72)

分类: Android平台

2013-07-02 10:42:41

Android 4.1.2系统中,通过 设置->声音->触摸时震动 选项可以禁止触摸震动,但无法禁止解锁震动和关机震动,比较烦,于是研究了一番,修改framework和services就可以让该设置同样对解锁和关机震动有效。

首先修改frameworks/base/services/java/com/android/server/pm/ShutdownThread.java以关联 触摸时震动 和关机震动,关机时会调用
rebootOrShutdown函数,我们可以看到在该函数中,如果SHUTDOWN_VIBRATE_MS大于0,则创建一个SystemVibrator,执行震动效果,所以我们在此处修改即可:

  1. /**
  2.  * Do not call this directly. Use {@link #reboot(Context, String, boolean)}
  3.  * or {@link #shutdown(Context, boolean)} instead.
  4.  *
  5.  * @param reboot true to reboot or false to shutdown
  6.  * @param reason reason for reboot
  7.  */
  8. public static void rebootOrShutdown(boolean reboot, String reason) {
  9.     if (reboot) {
  10.         Log.i(TAG, "Rebooting, reason: " + reason);
  11.         try {
  12.             PowerManagerService.lowLevelReboot(reason);
  13.         } catch (Exception e) {
  14.             Log.e(TAG, "Reboot failed, will attempt shutdown instead", e);
  15.         }
  16.     } else if (SHUTDOWN_VIBRATE_MS > 0) {
  17.         boolean vibrateEnabled = Settings.System.getInt(sInstance.mContext.getContentResolver(),
  18.                 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 1;
  19.         if (vibrateEnabled) {
  20.             // vibrate before shutting down
  21.             Vibrator vibrator = new SystemVibrator();
  22.             try {
  23.                 vibrator.vibrate(SHUTDOWN_VIBRATE_MS);
  24.             } catch (Exception e) {
  25.                 // Failure to vibrate shouldn't interrupt shutdown. Just log it.
  26.                 Log.w(TAG, "Failed to vibrate during shutdown.", e);
  27.             }

  28.             // vibrator is asynchronous so we need to wait to avoid shutting down too soon.
  29.             try {
  30.                 Thread.sleep(SHUTDOWN_VIBRATE_MS);
  31.             } catch (InterruptedException unused) {
  32.             }
  33.         }
  34.     }

  35.     // Shutdown power
  36.     Log.i(TAG, "Performing low-level shutdown...");
  37.     PowerManagerService.lowLevelShutdown();
  38. }
对于解锁震动,则需要修改frameworks/base/core/java/com/android/internal/widget/multiwaveview/GlowPadView.java文件,我们可以看到GlowPadView类有一个setVibrateEnabled函数,即是设置该控件是否震动,而解锁界面则使用了该控件,所以我们只需在该类初始化的时候检测是否设置了触摸时震动选项,而后设置该控件是否可以震动即可:

  1. public GlowPadView(Context context, AttributeSet attrs) {
  2.         super(context, attrs);
  3.         Resources res = context.getResources();

  4.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GlowPadView);
  5.         mInnerRadius = a.getDimension(R.styleable.GlowPadView_innerRadius, mInnerRadius);
  6.         mOuterRadius = a.getDimension(R.styleable.GlowPadView_outerRadius, mOuterRadius);
  7.         mSnapMargin = a.getDimension(R.styleable.GlowPadView_snapMargin, mSnapMargin);
  8.         mVibrationDuration = a.getInt(R.styleable.GlowPadView_vibrationDuration,
  9.                 mVibrationDuration);
  10.         mFeedbackCount = a.getInt(R.styleable.GlowPadView_feedbackCount,
  11.                 mFeedbackCount);
  12.         mHandleDrawable = new TargetDrawable(res,
  13.                 a.peekValue(R.styleable.GlowPadView_handleDrawable).resourceId);
  14.         mHandleDrawable.setState(TargetDrawable.STATE_INACTIVE);
  15.         mOuterRing = new TargetDrawable(res,
  16.                 getResourceId(a, R.styleable.GlowPadView_outerRingDrawable));

  17.         mAlwaysTrackFinger = a.getBoolean(R.styleable.GlowPadView_alwaysTrackFinger, false);

  18.         int pointId = getResourceId(a, R.styleable.GlowPadView_pointDrawable);
  19.         Drawable pointDrawable = pointId != 0 ? res.getDrawable(pointId) : null;
  20.         mGlowRadius = a.getDimension(R.styleable.GlowPadView_glowRadius, 0.0f);

  21.         TypedValue outValue = new TypedValue();

  22.         // Read array of target drawables
  23.         if (a.getValue(R.styleable.GlowPadView_targetDrawables, outValue)) {
  24.             internalSetTargetResources(outValue.resourceId);
  25.         }
  26.         if (mTargetDrawables == null || mTargetDrawables.size() == 0) {
  27.             throw new IllegalStateException("Must specify at least one target drawable");
  28.         }

  29.         // Read array of target descriptions
  30.         if (a.getValue(R.styleable.GlowPadView_targetDescriptions, outValue)) {
  31.             final int resourceId = outValue.resourceId;
  32.             if (resourceId == 0) {
  33.                 throw new IllegalStateException("Must specify target descriptions");
  34.             }
  35.             setTargetDescriptionsResourceId(resourceId);
  36.         }

  37.         // Read array of direction descriptions
  38.         if (a.getValue(R.styleable.GlowPadView_directionDescriptions, outValue)) {
  39.             final int resourceId = outValue.resourceId;
  40.             if (resourceId == 0) {
  41.                 throw new IllegalStateException("Must specify direction descriptions");
  42.             }
  43.             setDirectionDescriptionsResourceId(resourceId);
  44.         }

  45.         a.recycle();

  46.         // Use gravity attribute from LinearLayout
  47.         a = context.obtainStyledAttributes(attrs, android.R.styleable.LinearLayout);
  48.         mGravity = a.getInt(android.R.styleable.LinearLayout_gravity, Gravity.TOP);
  49.         a.recycle();

  50.         boolean vibrateEnabled = Settings.System.getInt(context.getContentResolver(),
  51.                 Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) == 1;
  52.         setVibrateEnabled(vibrateEnabled ? mVibrationDuration > 0 : false);

  53.         assignDefaultsIfNeeded();

  54.         mPointCloud = new PointCloud(pointDrawable);
  55.         mPointCloud.makePointCloud(mInnerRadius, mOuterRadius);
  56.         mPointCloud.glowManager.setRadius(mGlowRadius);
  57.     }
通过以上两处改动,即可实现 触摸时震动 选项对解锁震动和关机震动同样生效。修改后编译,将生成的framework.jar,framework2.jar和services.jar放到/system/framework目录下,并修改其权限为644,重启即可生效。


阅读(10737) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~