前言廢話
一直以來都是從事應用層的開發(fā),也沒有什么機會接觸到怎么修改系統(tǒng)層源碼怎么編譯整個系統(tǒng),直到現(xiàn)在在的公司,是從事手機研發(fā)和智能后視鏡的研發(fā)才有這個機會和親眼目睹一整套從修改底源碼定制root到編譯系統(tǒng)出來再到應用層的開發(fā)是怎么工作起來的,因為自己也蠢蠢欲動開始初次嘗試這些工作,有點小激動...........,廢話不說了開始,過程很痛苦.....
未修改前這個原生是長什么樣子的
需求需要修改的樣子
思路
- 在應用層先實現(xiàn)這個dialog的效果
- 找到恢復出廠設(shè)置所在的源碼位置,把我們實現(xiàn)好的dialog替換原生的dialog
在應用層實現(xiàn),這里只粘貼部分代碼
自定義的progress
package com.haisheng.music.circularprogress;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Property;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import com.haisheng.music.R;
/*
*
*@author luhaisheng 實現(xiàn)跟原生loading效果一樣的progress CircularProgressView
*@time 2016/10/10 11:40
*/
public class CircularProgressView extends View {
private static final Interpolator ANGLE_INTERPOLATOR = new LinearInterpolator();
private static final Interpolator SWEEP_INTERPOLATOR = new AccelerateDecelerateInterpolator();
private int angleAnimatorDuration;
private int sweepAnimatorDuration;
private int minSweepAngle;
private float mBorderWidth;
private final RectF fBounds = new RectF();
private ObjectAnimator mObjectAnimatorSweep;
private ObjectAnimator mObjectAnimatorAngle;
private boolean mModeAppearing = true;
private Paint mPaint;
private float mCurrentGlobalAngleOffset;
private float mCurrentGlobalAngle;
private float mCurrentSweepAngle;
private boolean mRunning;
private int[] mColors;
private int mCurrentColorIndex;
private int mNextColorIndex;
public CircularProgressView(Context context) {
this(context, null);
}
public CircularProgressView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircularProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(
attrs,
R.styleable.CircularProgressView,
defStyleAttr, 0);
mBorderWidth = a.getDimension(
R.styleable.CircularProgressView_borderWidth,
5);
angleAnimatorDuration = a.getInt(
R.styleable.CircularProgressView_angleAnimationDurationMillis,
2000);
sweepAnimatorDuration = a.getInt(
R.styleable.CircularProgressView_sweepAnimationDurationMillis,
900);
minSweepAngle = a.getInt(
R.styleable.CircularProgressView_minSweepAngle,
30);
int colorArrayId = a.getResourceId(R.styleable.CircularProgressView_colorSequence,
R.array.circular_default_color_sequence);
if (isInEditMode()) {
mColors = new int[4];
mColors[0] = getResources().getColor(R.color.circular_blue);
mColors[1] = getResources().getColor(R.color.circular_blue);
mColors[2] = getResources().getColor(R.color.circular_blue);
mColors[3] = getResources().getColor(R.color.circular_blue);
} else {
mColors = getResources().getIntArray(colorArrayId);
}
a.recycle();
mCurrentColorIndex = 0;
mNextColorIndex = 1;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Cap.ROUND);
mPaint.setStrokeWidth(mBorderWidth);
mPaint.setColor(mColors[mCurrentColorIndex]);
setupAnimations();
}
private void innerStart() {
if (isRunning()) {
return;
}
mRunning = true;
mObjectAnimatorAngle.start();
mObjectAnimatorSweep.start();
invalidate();
}
private void innerStop() {
if (!isRunning()) {
return;
}
mRunning = false;
mObjectAnimatorAngle.cancel();
mObjectAnimatorSweep.cancel();
invalidate();
}
private boolean isRunning() {
return mRunning;
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == VISIBLE) {
innerStart();
} else {
innerStop();
}
}
@Override
protected void onAttachedToWindow() {
innerStart();
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
innerStop();
super.onDetachedFromWindow();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
fBounds.left = mBorderWidth / 2f + .5f;
fBounds.right = w - mBorderWidth / 2f - .5f;
fBounds.top = mBorderWidth / 2f + .5f;
fBounds.bottom = h - mBorderWidth / 2f - .5f;
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
float startAngle = mCurrentGlobalAngle - mCurrentGlobalAngleOffset;
float sweepAngle = mCurrentSweepAngle;
if (mModeAppearing) {
mPaint.setColor(gradient(mColors[mCurrentColorIndex], mColors[mNextColorIndex],
mCurrentSweepAngle / (360 - minSweepAngle * 2)));
sweepAngle += minSweepAngle;
} else {
startAngle = startAngle + sweepAngle;
sweepAngle = 360 - sweepAngle - minSweepAngle;
}
canvas.drawArc(fBounds, startAngle, sweepAngle, false, mPaint);
}
private static int gradient(int color1, int color2, float p) {
int r1 = (color1 & 0xff0000) >> 16;
int g1 = (color1 & 0xff00) >> 8;
int b1 = color1 & 0xff;
int r2 = (color2 & 0xff0000) >> 16;
int g2 = (color2 & 0xff00) >> 8;
int b2 = color2 & 0xff;
int newr = (int) (r2 * p + r1 * (1 - p));
int newg = (int) (g2 * p + g1 * (1 - p));
int newb = (int) (b2 * p + b1 * (1 - p));
return Color.argb(255, newr, newg, newb);
}
private void toggleAppearingMode() {
mModeAppearing = !mModeAppearing;
if (mModeAppearing) {
mCurrentColorIndex = ++mCurrentColorIndex % mColors.length;
mNextColorIndex = ++mNextColorIndex % mColors.length;
mCurrentGlobalAngleOffset = (mCurrentGlobalAngleOffset + minSweepAngle * 2) % 360;
}
}
private Property<CircularProgressView, Float> mAngleProperty = new Property<CircularProgressView, Float>(Float.class, "angle") {
@Override
public Float get(CircularProgressView object) {
return object.getCurrentGlobalAngle();
}
@Override
public void set(CircularProgressView object, Float value) {
object.setCurrentGlobalAngle(value);
}
};
private Property<CircularProgressView, Float> mSweepProperty = new Property<CircularProgressView, Float>(Float.class, "arc") {
@Override
public Float get(CircularProgressView object) {
return object.getCurrentSweepAngle();
}
@Override
public void set(CircularProgressView object, Float value) {
object.setCurrentSweepAngle(value);
}
};
private void setupAnimations() {
mObjectAnimatorAngle = ObjectAnimator.ofFloat(this, mAngleProperty, 360f);
mObjectAnimatorAngle.setInterpolator(ANGLE_INTERPOLATOR);
mObjectAnimatorAngle.setDuration(angleAnimatorDuration);
mObjectAnimatorAngle.setRepeatMode(ValueAnimator.RESTART);
mObjectAnimatorAngle.setRepeatCount(ValueAnimator.INFINITE);
mObjectAnimatorSweep = ObjectAnimator.ofFloat(this, mSweepProperty, 360f - minSweepAngle * 2);
mObjectAnimatorSweep.setInterpolator(SWEEP_INTERPOLATOR);
mObjectAnimatorSweep.setDuration(sweepAnimatorDuration);
mObjectAnimatorSweep.setRepeatMode(ValueAnimator.RESTART);
mObjectAnimatorSweep.setRepeatCount(ValueAnimator.INFINITE);
mObjectAnimatorSweep.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
toggleAppearingMode();
}
});
}
public void setCurrentGlobalAngle(float currentGlobalAngle) {
mCurrentGlobalAngle = currentGlobalAngle;
invalidate();
}
public float getCurrentGlobalAngle() {
return mCurrentGlobalAngle;
}
public void setCurrentSweepAngle(float currentSweepAngle) {
mCurrentSweepAngle = currentSweepAngle;
invalidate();
}
public float getCurrentSweepAngle() {
return mCurrentSweepAngle;
}
}
自定義的CustomWaitDialog
package com.haisheng.music.circularprogress;
import android.app.Dialog;
import android.content.Context;
import android.widget.TextView;
import com.haisheng.music.R;
public class CustomWaitDialog extends Dialog {
private TextView nTextView;
private CharSequence nText;
private CustomWaitDialog(Context context) {
this(context, 0);
}
private CustomWaitDialog(Context context, int theme) {
super(context, R.style.CustomDialogHolo);
setContentView(R.layout.custom_wait_dialog_layout);
nTextView = (TextView) findViewById(R.id.text_off_or_reboot);
setCancelable(false);
setCanceledOnTouchOutside(false);
}
public void setText(CharSequence text) {
nText = text;
}
@Override
public void show() {
nTextView.setText(nText);
super.show();
}
public static class Builder {
private CustomWaitDialog mDialog;
public Builder(Context context) {
mDialog = new CustomWaitDialog(context);
}
public Builder setText(CharSequence text) {
mDialog.setText(text);
return this;
}
public CustomWaitDialog build() {
return mDialog;
}
public void setCanceledOnTouchOutside(boolean flag) {
mDialog.setCanceledOnTouchOutside(flag);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="240dp"
android:layout_height="112dp"
android:background="@drawable/laun_po_bg"
>
<com.example.lu.myapplication.CircularProgressView
android:id="@+id/pv"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_centerVertical="true"
android:layout_marginLeft="32dp"
app:borderWidth="3.34dp"
app:colorSequence="@array/circular_default_color_sequence"
app:angleAnimationDurationMillis="2000"
app:sweepAnimationDurationMillis="900"
/>
<TextView
android:id="@+id/text_off_or_reboot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/pv"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:text="正在恢復出廠設(shè)置"
android:textColor="#666666"
android:textSize="16sp"/>
</RelativeLayout>
CustomWaitDialog的簡單使用
new CustomWaitDialog.Builder(this).setText("正在恢復出廠設(shè)置").build().show();
效果
在應用層實現(xiàn)很簡單,也能實現(xiàn)預期的效果,接下來查看源碼恢復出廠設(shè)置的代碼所在的位置,在這里我所修改的是MTK提供的android 6.0源碼,其他的改動位置估計也差不多,但要實踐才知道。
找到位置\alps\frameworks\base\services\core\java\com\android\server\power目錄下的ShutdownThread.java文件
-
分析這個類的源碼。這里順便說 ,我還不知道有什么比較好的ide查看源碼,所以我是用vim打開的。
1、查看beginShutdownSequence()代碼..... ...... // Throw up a system dialog to indicate the device is rebooting / shutting down. ProgressDialog pd = new ProgressDialog(context); // Path 1: Reboot to recovery and install the update // Condition: mRebootReason == REBOOT_RECOVERY and mRebootUpdate == True // (mRebootUpdate is set by checking if /cache/recovery/uncrypt_file exists.) // UI: progress bar // // Path 2: Reboot to recovery for factory reset // Condition: mRebootReason == REBOOT_RECOVERY // UI: spinning circle only (no progress bar) // // Path 3: Regular reboot / shutdown // Condition: Otherwise // UI: spinning circle only (no progress bar) if (PowerManager.REBOOT_RECOVERY.equals(mRebootReason)) { mRebootUpdate = new File(UNCRYPT_PACKAGE_FILE).exists(); if (mRebootUpdate) { pd.setTitle(context.getText(com.android.internal.R.string.reboot_to_update_title)); pd.setMessage(context.getText( com.android.internal.R.string.reboot_to_update_prepare)); pd.setMax(100); pd.setProgressNumberFormat(null); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setProgress(0); pd.setIndeterminate(false); } else { // Factory reset path. Set the dialog message accordingly. pd.setTitle(context.getText(com.android.internal.R.string.reboot_to_reset_title)); pd.setMessage(context.getText( com.android.internal.R.string.reboot_to_reset_message)); pd.setIndeterminate(true); } } else { pd.setTitle(context.getText(com.android.internal.R.string.power_off)); pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress)); pd.setIndeterminate(true); } pd.setCancelable(false); pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); // start the thread that initiates shutdown sInstance.mContext = context; sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); sInstance.mHandler = new Handler() { }; beginAnimationTime = 0; boolean mShutOffAnimation = configShutdownAnimation(context); int screenTurnOffTime = getScreenTurnOffTime(context); synchronized (mEnableAnimatingSync) { if (mEnableAnimating) { if (mShutOffAnimation) { Log.d(TAG, "mIBootAnim.isCustBootAnim() is true"); bootanimCust(context); } else { pd.show(); sInstance.mProgressDialog = pd; } sInstance.mHandler.postDelayed(mDelayDim, screenTurnOffTime); } }
分析:ProgressDialog 是系統(tǒng)的dialog,然后稍微往下看到.show()方法才是重點,這里說明了什么時候show出來,show出來之后又把這個dialog賦給了sInstance.mProgressDialog,接下來就要找這個sInstance.mProgressDialog,在什么時候給dismiss掉。
2、查看running()代碼
```java
......
......
//To void previous UI flick caused by shutdown animation stopping before BKL turning off
if (sInstance.mProgressDialog != null) {
sInstance.mProgressDialog.dismiss();
} else if (beginAnimationTime > 0) {
Log.i(TAG, "service.bootanim.exit = 1");
SystemProperties.set("service.bootanim.exit", "1");
}
.......
.......
分析:順利找到dismiss的位置。接下來就是修改這兩個位置
開始修改ShutdownThread.java 這個類的源碼
- 申明全局變量CustomWaitDialog
// IPO private static CustomWaitDialog pd = null;
- 修改 beginShutdownSequence()方法
......
......
//2016-9-24 luhaisheng
// Throw up a system dialog to indicate the device is rebooting / shutting down.
// ProgressDialog pd = new ProgressDialog(context);
// Path 1: Reboot to recovery and install the update
// Condition: mRebootReason == REBOOT_RECOVERY and mRebootUpdate == True
// (mRebootUpdate is set by checking if /cache/recovery/uncrypt_file exists.)
// UI: progress bar
//
// Path 2: Reboot to recovery for factory reset
// Condition: mRebootReason == REBOOT_RECOVERY
// UI: spinning circle only (no progress bar)
//
// Path 3: Regular reboot / shutdown
// Condition: Otherwise
// UI: spinning circle only (no progress bar)
if (PowerManager.REBOOT_RECOVERY.equals(mRebootReason)) {
mRebootUpdate = new File(UNCRYPT_PACKAGE_FILE).exists();
if (mRebootUpdate) {
// pd.setTitle(context.getText(com.android.internal.R.string.reboot_to_update_title));
// pd.setMessage(context.getText(
// com.android.internal.R.string.reboot_to_update_prepare));
// pd.setMax(100);
// pd.setProgressNumberFormat(null);
// pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// pd.setProgress(0);
// pd.setIndeterminate(false);
} else {
// Factory reset path. Set the dialog message accordingly.
// pd.setTitle(context.getText(com.android.internal.R.string.reboot_to_reset_title));
// pd.setMessage(context.getText(
// com.android.internal.R.string.reboot_to_reset_message));
// pd.setIndeterminate(true);
}
} else {
// pd.setTitle(context.getText(com.android.internal.R.string.power_off));
// pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));
// pd.setIndeterminate(true);
}
// pd.setCancelable(false);
// pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
// start the thread that initiates shutdown
sInstance.mContext = context;
sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
sInstance.mHandler = new Handler() {
};
beginAnimationTime = 0;
boolean mShutOffAnimation = configShutdownAnimation(context);
int screenTurnOffTime = getScreenTurnOffTime(context);
synchronized (mEnableAnimatingSync) {
if (mEnableAnimating) {
if (mShutOffAnimation) {
Log.d(TAG, "mIBootAnim.isCustBootAnim() is true");
bootanimCust(context);
} else {
pd = new CustomWaitDialog.Builder(context).build();
//luhaisheng 2016/10/10
if(mReboot){
pd.setText(context.getText(com.android.internal.R.string.custom_text_reboot_ing));
}else{
pd.setText(context.getText(com.android.internal.R.string.custom_text_off_ing));
}
pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
/* To fix video+UI+blur flick issue */
pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
pd.show();
//luhaisheng 2016/10/10
// pd.show();
// sInstance.mProgressDialog = pd;
}
sInstance.mHandler.postDelayed(mDelayDim, screenTurnOffTime);
}
}
在這里主要修改三個地方,第一個是注釋掉原來pd的地方,第二個是在
if(mReboot){
pd.setText(context.getText(com.android.internal.R.string.custom_text_reboot_ing));
}else{
pd.setText(context.getText(com.android.internal.R.string.custom_text_off_ing)); }
通過mReboot判斷是否重啟,第三個是注釋掉原來的show方法。
- 修改 running()
//To void previous UI flick caused by shutdown animation stopping before BKL turning off
// if (sInstance.mProgressDialog != null) {
// sInstance.mProgressDialog.dismiss();
//2016-09-24 luhaisheng
if(pd!=null){
pd.dismiss();
pd=null;
} else if (beginAnimationTime > 0) {
Log.i(TAG, "service.bootanim.exit = 1");
SystemProperties.set("service.bootanim.exit", "1");
}
在原來的判斷dimiss方法上修改。以上就是修改ShutdownThread.java所做的工作,看起來也不簡單,因為要分析源碼,而且不能像修改應用層那么便捷因為你還沒有引用資源,自定義的類,還要編譯系統(tǒng),刷機才能看到最終的效果,反正繼續(xù)往下做,至于為什么這么修改,有時間的同學自己研究下源碼,看這么修改是否有問題,也可以有其他的修改方式。
引入自定義類 CircularProgressView和CustomWaitDialog
*在alps\frameworks\base\services\core\java\com\android\server\power目錄下放置
CircularProgressView.java與CustomWaitDialog.java,也就是與修改的ShutdownThread.java同級。
*編輯CircularProgressView.java 修改導入的包名和引入的R。修改如下
package com.android.server.power;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Property;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import com.android.internal.R;
.....
分析:就修改了兩個地方,package 是com.android.server.power,和導入的R是com.android.internal.R,不然肯定出錯。
同理CustomWaitDialog.java 也是這么改的。
添加資源文件和修改資源文件(最最最麻煩的地方)
- 在alps\frameworks\base\core\res\res\drawable-hdpi目錄下放置資源圖片,我這里只需要放置一張
- 為添加的資源圖片文件添加id(id 是自己手動添加的 沒有ide 自動導入 很麻煩),編輯alps\frameworks\base\core\res\res\values 目錄下public.xml的文件,自增一個type="drawable" 的item。我的是
<public type="drawable" name="laun_po_bg" id="0x010800b6"/>
- 為布局文件中的控件TextView申明id text_off_or_reboot。編譯alps\frameworks\base\core\res\res\目錄下ids.xml的文件,編輯如下:
<!--2016 1009 luhaisheng-->
<item type="id" name="text_off_or_reboot" />
<item type="id" name="pv" />
所以你自己定義的控件id都需要再ids.xml下做申明,還需要在public.xml下添加id,添加如下:
<public type="id" name="text_off_or_reboot" id="0x0102003e"/>
<public type="id" name="pv" id="0x0102003f"/>
- 與添加控件的id類似,所以自定義的style,layout,string,color等,都需要在相應的目錄下申明,然后在public.xml下添加id。我的public.xml如下所示:
<public type="style" name="CustomDialogHolo" id="0x010302e0"/>
<public type="layout" name="custom_wait_dialog_layout" id="0x01090018"/>
<public type="id" name="text_off_or_reboot" id="0x0102003e"/>
<public type="id" name="pv" id="0x0102003f"/>
<public type="string" name="custom_text_off_ing" id="0x01040019"/>
<public type="string" name="custom_text_reboot_ing" id="0x01040020"/>
<public type="drawable" name="laun_po_bg" id="0x010800b6"/>
<public type="attr" name="borderWidth" id="0x010104f2" />
<public type="attr" name="colorSequence" id="0x010104f3" />
<public type="attr" name="sweepAnimationDurationMillis" id="0x010104f4" />
<public type="attr" name="angleAnimationDurationMillis" id="0x010104f5" />
<public type="attr" name="minSweepAngle" id="0x010104f6" />
<public type="array" name="circular_default_color_sequence" id="0x01070006" />
<public type="color" name="circular_blue" id="0x0106001c" />
等資源添加確定無誤后,其實誰也不能確定,那么就開執(zhí)行編譯吧,因為出錯看錯就好了,像這種一般都是添加資源時候漏了申明某個id,或者id沖突之類的,我就錯了好幾十次了,保存好所有修改,后在framework/base/core/res/res/ 下mm編譯一次,無誤后在跟目錄下make update-api,update成功之后就可以編譯整個系統(tǒng)了。
- 在跟目錄下執(zhí)行 source build/envsetup.sh
- lunch 找到與你硬件相對于的版本
- make -j8 開始編譯整個系統(tǒng)