新增功能,基本上国内手机产商都会加上的功能,其实主要做的就是桌面图标重新排列的算法。
Launcher.java继承SensorEventListener重写监听接口
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
int sensorType = event.sensor.getType();
float[] values = event.values;
long vibrateTime = 200L;
if (sensorType == Sensor.TYPE_ACCELEROMETER)
{
int value = 15;
if ((Math.abs(values[0]) > value || Math.abs(values[1]) > value || Math
.abs(values[2]) > value + 10))
{
if (mWorkspace.isInOverviewMode()){
if (CommonUtils.isFastDoubleClick()) {
} else {
mWorkspace.rearrangeApps();
mVibrator.vibrate(vibrateTime);
}
}
}
}
}
CommonUtils 这个类是防止连续触发用的
class CommonUtils {
private static long lastClickTime;
public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if ( 0 < timeD && timeD < 1000) {
return true;
}
lastClickTime = time;
return false;
}
}
workspace.java
public void rearrangeApps(){
int count = getChildCount();
for (int i=0 ; i<count ; i++ ){
long screenID = getScreenIdForPageIndex(i);
if (screenID == CUSTOM_CONTENT_SCREEN_ID){
continue;
}
if (i == 1){
CellLayout cl = (CellLayout)getChildAt(i);
cl.setUseTempCoords(false);
cl.rearrangeHomePage();
} else {
CellLayout cl = (CellLayout)getChildAt(i);
cl.setUseTempCoords(false);
cl.rearrangeAppItems();
}
}
}
CellLayout.java
public void rearrangeAppItems(){
ArrayList<View> list = new ArrayList<View>();
ArrayList<String> blackList = new ArrayList<String>();
int neadReoderSize = getShortcutsAndWidgets().getChildCount();
for (int i=0; i<getShortcutsAndWidgets().getChildCount(); i++){
View tempView = getShortcutsAndWidgets().getChildAt(i);
list.add(tempView);
ItemInfo info = (ItemInfo) tempView.getTag();
if (info.spanX > 1 || info.spanY > 1){
neadReoderSize --;
for (int y=info.cellY; y<info.cellY+info.spanY; y++){
for (int x=info.cellX; x<info.cellX+info.spanX; x++){
int a = x + y*4;
blackList.add(a + "");
if (a < neadReoderSize) {
neadReoderSize ++;
}
}
}
}
}
for (int i=0; i<list.size(); i++){
ItemInfo info = (ItemInfo) list.get(i).getTag();
if (info.spanX > 1 || info.spanY > 1){
} else {
int oldPosition = info.cellX + info.cellY*4;
if (oldPosition < neadReoderSize ) {
blackList.add(oldPosition + "");
info.neadReoder = false;
} else {
info.neadReoder = true;
}
}
}
// getShortcutsAndWidgets().removeAllViews();
int position = 0;
int newX, newY, rank;
rank = 0;
InvariantDeviceProfile grid = LauncherAppState.getInstance().getInvariantDeviceProfile();
for (int i=0; i<list.size(); i++){
View v = list.get(i);
if (v != null){
// CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
newX = position % grid.numColumns;
newY = position / grid.numColumns;
ItemInfo info = (ItemInfo) v.getTag();
if (blackList != null){
while(blackList.contains(position + "")){
newX ++;
rank ++;
position++;
if (newX > 3){
newY++;
newX = 0;
}
}
}
if (info.cellX != newX || info.cellY != newY || info.rank != rank) {
if (info.spanX == 1 && info.spanY == 1 & info.neadReoder ){
info.cellX = newX;
info.cellY = newY;
info.rank = rank;
if (animateChildToPosition(v, info.cellX, info.cellY, REORDER_ANIMATION_DURATION, 0, true, true)){
Log.d("txk", "info.title=" + info.title + "; info.cellX=" + info.cellX + "; info.cellY=" + info.cellY + ";newX=" + newX + ";newY=" + newY);
LauncherModel.modifyItemInDatabase(getContext(), info,
info.container, info.screenId, info.cellX, info.cellY, info.spanX, info.spanY);
}
} else {
rank --;
position --;
}
}
}
rank ++;
position++;
}
}
public void rearrangeHomePage(){
ArrayList<View> list = new ArrayList<View>();
ArrayList<String> blackList = new ArrayList<String>();
int neadReoderSize = 20 - getShortcutsAndWidgets().getChildCount();
for (int i=0; i<getShortcutsAndWidgets().getChildCount(); i++){
View tempView = getShortcutsAndWidgets().getChildAt(i);
list.add(tempView);
ItemInfo info = (ItemInfo) tempView.getTag();
if (info.spanX > 1 || info.spanY > 1){
neadReoderSize ++;
for (int y=info.cellY; y<info.cellY+info.spanY; y++){
for (int x=info.cellX; x<info.cellX+info.spanX; x++){
int a = x + y*4;
blackList.add(a + "");
if (a > neadReoderSize) {
neadReoderSize --;
}
}
}
}
}
for (int i=0; i<list.size(); i++){
ItemInfo info = (ItemInfo) list.get(i).getTag();
if (info.spanX > 1 || info.spanY > 1){
} else {
int oldPosition = info.cellX + info.cellY*4;
if (oldPosition > neadReoderSize ) {
blackList.add(oldPosition + "");
info.neadReoder = false;
} else {
info.neadReoder = true;
}
}
}
// getShortcutsAndWidgets().removeAllViews();
InvariantDeviceProfile grid = LauncherAppState.getInstance().getInvariantDeviceProfile();
int position = grid.numRows * grid.numColumns - 1;
int newX, newY, rank;
rank = grid.numRows * grid.numColumns - 1;
for (int i=0; i<list.size(); i++){
View v = list.get(i);
if (v != null){
// CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
newX = position % grid.numColumns;
newY = position / grid.numColumns;
ItemInfo info = (ItemInfo) v.getTag();
if (blackList != null){
while(blackList.contains(position + "")){
newX --;
rank --;
position --;
if (newX < 0 ){
newY--;
newX = 3;
}
}
}
if (info.cellX != newX || info.cellY != newY || info.rank != rank) {
if (info.spanX == 1 && info.spanY == 1 & info.neadReoder ){
info.cellX = newX;
info.cellY = newY;
info.rank = rank;
if (animateChildToPosition(v, info.cellX, info.cellY, REORDER_ANIMATION_DURATION, 0, true, true)){
LauncherModel.modifyItemInDatabase(getContext(), info,
info.container, info.screenId, info.cellX, info.cellY, info.spanX, info.spanY);
}
} else {
rank ++;
position ++;
}
}
}
rank --;
position--;
}
}