/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.walcl.ulauncher; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import mobi.urika.android.content.LauncherIntent; import mobi.urika.android.content.LauncherMetadata; import cn.walcl.ulauncher.ItemInfo.EditAction; import cn.walcl.ulauncher.actions.DefaultAction; import cn.walcl.ulauncher.actions.LauncherActions; import cn.walcl.ulauncher.appdb.AppDB; import cn.walcl.ulauncher.quickactionbar.ActionItem; import cn.walcl.ulauncher.quickactionbar.QuickAction; import cn.walcl.ulauncher.settings.LauncherSettings; import cn.walcl.ulauncher.settings.Preferences; import cn.walcl.ulauncher.settings.LauncherSettings.Favorites; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.SearchManager; import android.app.WallpaperManager; import android.appwidget.AppWidgetHostView; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProviderInfo; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.Intent.ShortcutIconResource; import android.content.pm.PackageManager; import android.content.pm.ProviderInfo; import android.content.pm.ResolveInfo; import android.content.res.Configuration; import android.content.res.Resources; import android.content.res.TypedArray; import android.database.ContentObserver; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Parcelable; import android.provider.LiveFolders; import android.text.Selection; import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.text.method.TextKeyListener; import android.util.Log; import android.view.Display; import android.view.HapticFeedbackConstants; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; /** * Default launcher application. */ public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher { static final String TAG = "Launcher"; static final boolean LOGD = false; static final boolean PROFILE_STARTUP = false; private static final int WALLPAPER_SCREENS_SPAN = 2; private static final int MENU_GROUP_HOMESCREEN = 1; private static final int MENU_GROUP_DRAWER = MENU_GROUP_HOMESCREEN + 1; private static final int MENU_GROUP_ADD = MENU_GROUP_DRAWER +1; private static final int MENU_ADD = Menu.FIRST + 1; private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1; private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1; private static final int MENU_DRAWER_ADD_FOLDER = MENU_SEARCH + 1; private static final int REQUEST_CREATE_SHORTCUT = 1; private static final int REQUEST_CREATE_LIVE_FOLDER = 4; private static final int REQUEST_CREATE_APPWIDGET = 5; private static final int REQUEST_PICK_APPLICATION = 6; private static final int REQUEST_PICK_SHORTCUT = 7; private static final int REQUEST_PICK_LIVE_FOLDER = 8; private static final int REQUEST_PICK_APPWIDGET = 9; private static final int REQUEST_PICK_WALLPAPER = 10; private static final int REQUEST_PICK_ANYCUT=11; static final int REQUEST_EDIT_SHIRTCUT = 12; private static final int REQUEST_PICK_LIVE_FOLDER_DRAWER = 13; private static final int REQUEST_CREATE_LIVE_FOLDER_DRAWER = 14; static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate"; static final int SCREEN_COUNT = 5; static final int DEFAULT_SCREEN = 2; static final int NUMBER_CELLS_X = 4; static final int NUMBER_CELLS_Y = 4; static final int DIALOG_CREATE_SHORTCUT = 1; static final int DIALOG_RENAME_FOLDER = 2; private static final String PREFERENCES = "launcher.preferences"; // Type: int private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen"; // Type: boolean private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder"; // Type: long private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder"; // Type: int private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen"; // Type: int private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX"; // Type: int private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY"; // Type: int private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX"; // Type: int private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY"; // Type: int private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX"; // Type: int private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY"; // Type: int[] private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells"; // Type: boolean private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder"; // Type: long private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id"; static final int APPWIDGET_HOST_ID = 1024; private static final Object sLock = new Object(); private static int sScreen = DEFAULT_SCREEN; private final BroadcastReceiver mCloseSystemDialogsReceiver = new CloseSystemDialogsIntentReceiver(); private final ContentObserver mWidgetObserver = new AppWidgetResetObserver(); private LayoutInflater mInflater; private DragController mDragController; private Workspace mWorkspace; private AppWidgetManager mAppWidgetManager; private LauncherAppWidgetHost mAppWidgetHost; private CellLayout.CellInfo mAddItemCellInfo; private CellLayout.CellInfo mMenuAddInfo; private final int[] mCellCoordinates = new int[2]; private FolderInfo mFolderInfo; private DeleteZone mDeleteZone; private HandleView mHandleView; private AllAppsView mAllAppsGrid; private Bundle mSavedState; private SpannableStringBuilder mDefaultKeySsb = null; private boolean mWorkspaceLoading = true; private boolean mIsWidgetEditMode = false; private ResizeViewHandler mScreensEditor=null; private LauncherAppWidgetInfo mEditingAppWidget = null; private boolean mPaused = true; private boolean mRestoring; private boolean mWaitingForResult; private boolean mOnResumeNeedsLoad; private Bundle mSavedInstanceState; private LauncherModel mModel; private IconCache mIconCache; private AppDB mAppDB; private DragLayer mDragLayer; private static LocaleConfiguration sLocaleConfiguration = null; private final ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>(); private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>(); private ImageView mPreviousView; private ImageView mNextView; private String[] mHotseatConfig = null; private Intent[] mHotseats = null; private Drawable[] mHotseatIcons = null; private CharSequence[] mHotseatLabels = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LauncherApplication app = ((LauncherApplication)getApplication()); mModel = app.setLauncher(this); mIconCache = app.getIconCache(); mAppDB = app.getAppDB(); mDragController = new DragController(this); mInflater = getLayoutInflater(); Preferences.getInstance().setLauncher(this); mAppWidgetManager = AppWidgetManager.getInstance(this); mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID); mAppWidgetHost.startListening(); if (PROFILE_STARTUP) { android.os.Debug.startMethodTracing( Environment.getExternalStorageDirectory() + "/launcher"); } loadHotseats(); //����locale��mcc��mnc�Ƿ�ı䣬����ı䣬����д������ //mcc��mobile country code(���Ҵ���China 460); mnc��mobile network code(�������) checkForLocaleChange(); //This allows such applications to have a virtual wallpaper that is larger than the physical setWallpaperDimension(); setContentView(R.layout.launcher); setupViews(); registerContentObservers(); lockAllApps(); mSavedState = savedInstanceState; restoreState(mSavedState); if (PROFILE_STARTUP) { android.os.Debug.stopMethodTracing(); } if (!mRestoring) { mModel.startLoader(this, true); } // For handling default keys mDefaultKeySsb = new SpannableStringBuilder(); Selection.setSelection(mDefaultKeySsb, 0); IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); registerReceiver(mCloseSystemDialogsReceiver, filter); LauncherActions.getInstance().init(this); } private void checkForLocaleChange() { if (sLocaleConfiguration == null) { new AsyncTask<Void, Void, LocaleConfiguration>() { @Override protected LocaleConfiguration doInBackground(Void... unused) { LocaleConfiguration localeConfiguration = new LocaleConfiguration(); readConfiguration(Launcher.this, localeConfiguration); return localeConfiguration; } @Override protected void onPostExecute(LocaleConfiguration result) { sLocaleConfiguration = result; checkForLocaleChange(); // recursive, but now with a locale configuration } }.execute(); return; } final Configuration configuration = getResources().getConfiguration(); final String previousLocale = sLocaleConfiguration.locale; final String locale = configuration.locale.toString(); final int previousMcc = sLocaleConfiguration.mcc; final int mcc = configuration.mcc; final int previousMnc = sLocaleConfiguration.mnc; final int mnc = configuration.mnc; boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc; if (localeChanged) { sLocaleConfiguration.locale = locale; sLocaleConfiguration.mcc = mcc; sLocaleConfiguration.mnc = mnc; mIconCache.flush(); loadHotseats(); final LocaleConfiguration localeConfiguration = sLocaleConfiguration; new Thread("WriteLocaleConfiguration") { @Override public void run() { mIconCache.flush(); mAppDB.updateLocale(locale); writeConfiguration(Launcher.this, localeConfiguration); } }.start(); } } private static class LocaleConfiguration { public String locale; public int mcc = -1; public int mnc = -1; } private static void readConfiguration(Context context, LocaleConfiguration configuration) { DataInputStream in = null; try { in = new DataInputStream(context.openFileInput(PREFERENCES)); configuration.locale = in.readUTF(); configuration.mcc = in.readInt(); configuration.mnc = in.readInt(); } catch (FileNotFoundException e) { // Ignore } catch (IOException e) { // Ignore } finally { if (in != null) { try { in.close(); } catch (IOException e) { // Ignore } } } } private static void writeConfiguration(Context context, LocaleConfiguration configuration) { DataOutputStream out = null; try { out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE)); out.writeUTF(configuration.locale); out.writeInt(configuration.mcc); out.writeInt(configuration.mnc); out.flush(); } catch (FileNotFoundException e) { // Ignore } catch (IOException e) { //noinspection ResultOfMethodCallIgnored context.getFileStreamPath(PREFERENCES).delete(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { // Ignore } } } } public IconCache getIconCache() { return mIconCache; } public LauncherModel getModel() { return mModel; } public AppDB getAppDB() { return mAppDB; } public AllAppsView getAllAppsView() { return mAllAppsGrid; } static int getScreen() { synchronized (sLock) { return sScreen; } } static void setScreen(int screen) { synchronized (sLock) { sScreen = screen; } } private void setWallpaperDimension() { WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE); Display display = getWindowManager().getDefaultDisplay(); boolean isPortrait = display.getWidth() < display.getHeight(); final int width = isPortrait ? display.getWidth() : display.getHeight(); final int height = isPortrait ? display.getHeight() : display.getWidth(); wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height); } // Note: This doesn't do all the client-id magic that BrowserProvider does // in Browser. (http://b/2425179) private Uri getDefaultBrowserUri() { String url = getString(R.string.default_browser_url); if (url.indexOf("{CID}") != -1) { url = url.replace("{CID}", "android-google"); } return Uri.parse(url); } // Load the Intent templates from arrays.xml to populate the hotseats. For // each Intent, if it resolves to a single app, use that as the launch // intent & use that app's label as the contentDescription. Otherwise, // retain the ResolveActivity so the user can pick an app. private void loadHotseats() { if (mHotseatConfig == null) { mHotseatConfig = getResources().getStringArray(R.array.hotseats); if (mHotseatConfig.length > 0) { mHotseats = new Intent[mHotseatConfig.length]; mHotseatLabels = new CharSequence[mHotseatConfig.length]; mHotseatIcons = new Drawable[mHotseatConfig.length]; } else { mHotseats = null; mHotseatIcons = null; mHotseatLabels = null; } TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons); for (int i=0; i<mHotseatConfig.length; i++) { // load icon for this slot; currently unrelated to the actual activity try { mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i); } catch (ArrayIndexOutOfBoundsException ex) { Log.w(TAG, "Missing hotseat_icons array item #" + i); mHotseatIcons[i] = null; } } hotseatIconDrawables.recycle(); } PackageManager pm = getPackageManager(); for (int i=0; i<mHotseatConfig.length; i++) { Intent intent = null; if (mHotseatConfig[i].equals("*BROWSER*")) { // magic value meaning "launch user's default web browser" // replace it with a generic web request so we can see if there is indeed a default String defaultUri = getString(R.string.default_browser_url); intent = new Intent( Intent.ACTION_VIEW, ((defaultUri != null) ? Uri.parse(defaultUri) : getDefaultBrowserUri()) ).addCategory(Intent.CATEGORY_BROWSABLE); // note: if the user launches this without a default set, she // will always be taken to the default URL above; this is // unavoidable as we must specify a valid URL in order for the // chooser to appear, and once the user selects something, that // URL is unavoidably sent to the chosen app. } else { try { intent = Intent.parseUri(mHotseatConfig[i], 0); } catch (java.net.URISyntaxException ex) { Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]); // bogus; leave intent=null } } if (intent == null) { mHotseats[i] = null; mHotseatLabels[i] = getText(R.string.activity_not_found); continue; } if (LOGD) { Log.d(TAG, "loadHotseats: hotseat " + i + " initial intent=[" + intent.toUri(Intent.URI_INTENT_SCHEME) + "]"); } ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); if (LOGD) { Log.d(TAG, "Best match for intent: " + bestMatch); Log.d(TAG, "All matches: "); for (ResolveInfo ri : allMatches) { Log.d(TAG, " --> " + ri); } } // did this resolve to a single app, or the resolver? if (allMatches.size() == 0 || bestMatch == null) { // can't find any activity to handle this. let's leave the // intent as-is and let Launcher show a toast when it fails // to launch. mHotseats[i] = intent; // set accessibility text to "Not installed" mHotseatLabels[i] = getText(R.string.activity_not_found); } else { boolean found = false; for (ResolveInfo ri : allMatches) { if (bestMatch.activityInfo.name.equals(ri.activityInfo.name) && bestMatch.activityInfo.applicationInfo.packageName .equals(ri.activityInfo.applicationInfo.packageName)) { found = true; break; } } if (!found) { if (LOGD) Log.d(TAG, "Multiple options, no default yet"); // the bestMatch is probably the ResolveActivity, meaning the // user has not yet selected a default // so: we'll keep the original intent for now mHotseats[i] = intent; // set the accessibility text to "Select shortcut" mHotseatLabels[i] = getText(R.string.title_select_shortcut); } else { // we have an app! // now reconstruct the intent to launch it through the front // door ComponentName com = new ComponentName( bestMatch.activityInfo.applicationInfo.packageName, bestMatch.activityInfo.name); mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com); // load the app label for accessibility mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm); } } if (LOGD) { Log.d(TAG, "loadHotseats: hotseat " + i + " final intent=[" + ((mHotseats[i] == null) ? "null" : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME)) + "] label=[" + mHotseatLabels[i] + "]" ); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { mWaitingForResult = false; // The pattern used here is that a user PICKs a specific application, // which, depending on the target, might need to CREATE the actual target. // For example, the user would PICK_SHORTCUT for "Music playlist", and we // launch over to the Music app to actually CREATE_SHORTCUT. if (resultCode == RESULT_OK && requestCode == REQUEST_EDIT_SHIRTCUT) { completeEditShirtcut(data); return; } if (resultCode == RESULT_OK && (mAddItemCellInfo != null || requestCode == REQUEST_PICK_LIVE_FOLDER_DRAWER || requestCode == REQUEST_CREATE_LIVE_FOLDER_DRAWER)) { switch (requestCode) { case REQUEST_PICK_APPLICATION: completeAddApplication(this, data, mAddItemCellInfo); break; case REQUEST_PICK_SHORTCUT: processShortcut(data); break; case REQUEST_PICK_ANYCUT: case REQUEST_CREATE_SHORTCUT: completeAddShortcut(data, mAddItemCellInfo); break; case REQUEST_PICK_LIVE_FOLDER_DRAWER: addLiveFolder(data, true); break; case REQUEST_PICK_LIVE_FOLDER: addLiveFolder(data, false); break; case REQUEST_CREATE_LIVE_FOLDER_DRAWER: completeAddLiveFolderDrawer(data); break; case REQUEST_CREATE_LIVE_FOLDER: completeAddLiveFolder(data, mAddItemCellInfo); break; case REQUEST_PICK_APPWIDGET: addAppWidget(data); break; case REQUEST_CREATE_APPWIDGET: completeAddAppWidget(data, mAddItemCellInfo); break; case REQUEST_PICK_WALLPAPER: // We just wanted the activity result here so we can clear mWaitingForResult break; } } else if ((requestCode == REQUEST_PICK_APPWIDGET || requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED && data != null) { // Clean up the appWidgetId if we canceled int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); if (appWidgetId != -1) { mAppWidgetHost.deleteAppWidgetId(appWidgetId); } } } @Override protected void onResume() { super.onResume(); mPaused = false; if (mRestoring || mOnResumeNeedsLoad) { mWorkspaceLoading = true; mModel.startLoader(this, true); mRestoring = false; mOnResumeNeedsLoad = false; } } @Override protected void onPause() { super.onPause(); mPaused = true; dismissPreview(mPreviousView); dismissPreview(mNextView); mDragController.cancelDrag(); } @Override public Object onRetainNonConfigurationInstance() { // Flag the loader to stop early before switching mModel.stopLoader(); mAllAppsGrid.surrender(); return Boolean.TRUE; } private void completeEditShirtcut(Intent data) { if (!data.hasExtra(CustomShirtcutActivity.EXTRA_APPLICATIONINFO)) return; long appInfoId = data.getLongExtra(CustomShirtcutActivity.EXTRA_APPLICATIONINFO, 0); if ( data.hasExtra(CustomShirtcutActivity.EXTRA_DRAWERINFO)) { List<ShortcutInfo> apps = mAppDB.getApps(new long[] {appInfoId}); if (apps.size() == 1) { IconItemInfo info = apps.get(0); Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); if (bitmap != null) { info.setIcon(bitmap); } else { bitmap = info.mIcon; } String title = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); info.setTitle(title); mAppDB.updateAppDisplay(appInfoId, title, bitmap); // Notify Model: Intent modelIntent = new Intent(AppDB.INTENT_DB_CHANGED); modelIntent.putExtra(AppDB.EXTRA_UPDATED, new long[] {info.id} ); sendBroadcast(modelIntent); } } else { ItemInfo ii = mModel.getItemInfoById(appInfoId); if (ii != null && ii instanceof IconItemInfo) { IconItemInfo info = (IconItemInfo)ii; Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); if (bitmap != null) { info.setIcon(bitmap); } info.setTitle(data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME)); if (data.hasExtra(Intent.EXTRA_SHORTCUT_INTENT) && info instanceof ShortcutInfo) ((ShortcutInfo)info).intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); LauncherModel.updateItemInDatabase(this, info); // Need to update the icon here! if (ii.container == Favorites.CONTAINER_DRAWER) { ArrayList<IconItemInfo> lst = new ArrayList<IconItemInfo>(); lst.add(info); mAllAppsGrid.updateApps(lst); } else { View v = mWorkspace.findViewWithTag(info); if (v instanceof BubbleTextView) ((BubbleTextView)v).updateFromItemInfo(mIconCache, info); } } } } // We can't hide the IME if it was forced open. So don't bother /* @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); WindowManager.LayoutParams lp = getWindow().getAttributes(); inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new android.os.Handler()) { protected void onReceiveResult(int resultCode, Bundle resultData) { Log.d(TAG, "ResultReceiver got resultCode=" + resultCode); } }); Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged"); } } */ private boolean acceptFilter() { final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); return !inputManager.isFullscreenMode(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { boolean handled = super.onKeyDown(keyCode, event); if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) { boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb, keyCode, event); if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) { // something usable has been typed - start a search // the typed text will be retrieved and cleared by // showSearchDialog() // If there are multiple keystrokes before the search dialog takes focus, // onSearchRequested() will be called for every keystroke, // but it is idempotent, so it's fine. return onSearchRequested(); } } // Eat the long press event so the keyboard doesn't come up. if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) { return true; } return false; // return handled; } private String getTypedText() { return mDefaultKeySsb.toString(); } private void clearTypedText() { mDefaultKeySsb.clear(); mDefaultKeySsb.clearSpans(); Selection.setSelection(mDefaultKeySsb, 0); } /** * Restores the previous state, if it exists. * * @param savedState The previous state. */ private void restoreState(Bundle savedState) { if (savedState == null) { return; } final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false); if (allApps) { showAllApps(false); } final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1); if (currentScreen > -1) { mWorkspace.setCurrentScreen(currentScreen); } final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1); if (addScreen > -1) { mAddItemCellInfo = new CellLayout.CellInfo(); final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; addItemCellInfo.valid = true; addItemCellInfo.screen = addScreen; addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X); addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y); addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X); addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y); addItemCellInfo.findVacantCellsFromOccupied( savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS), savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X), savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y)); mRestoring = true; } boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false); if (renameFolder) { long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID); mFolderInfo = mModel.getFolderById(this, sFolders, id); mRestoring = true; } } /** * Finds all the views we need and configure them properly. */ private void setupViews() { DragController dragController = mDragController; mDragLayer = (DragLayer) findViewById(R.id.drag_layer); mDragLayer.setDragController(dragController); mAllAppsGrid = (AllAppsView)mDragLayer.findViewById(R.id.all_apps_view); mAllAppsGrid.setLauncher(this); mAllAppsGrid.setDragController(dragController); ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window. // Manage focusability manually since this thing is always visible ((View) mAllAppsGrid).setFocusable(false); mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace); final Workspace workspace = mWorkspace; workspace.setHapticFeedbackEnabled(false); DeleteZone deleteZone = (DeleteZone) mDragLayer.findViewById(R.id.delete_zone); mDeleteZone = deleteZone; mHandleView = (HandleView) findViewById(R.id.all_apps_button); mHandleView.setLauncher(this); mHandleView.setOnClickListener(this); mHandleView.setOnLongClickListener(this); ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left); hotseatLeft.setContentDescription(mHotseatLabels[0]); hotseatLeft.setImageDrawable(mHotseatIcons[0]); ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right); hotseatRight.setContentDescription(mHotseatLabels[1]); hotseatRight.setImageDrawable(mHotseatIcons[1]); mPreviousView = (ImageView) mDragLayer.findViewById(R.id.previous_screen); mNextView = (ImageView) mDragLayer.findViewById(R.id.next_screen); Drawable previous = mPreviousView.getDrawable(); Drawable next = mNextView.getDrawable(); mWorkspace.setIndicators(previous, next); mPreviousView.setHapticFeedbackEnabled(false); mPreviousView.setOnLongClickListener(this); mNextView.setHapticFeedbackEnabled(false); mNextView.setOnLongClickListener(this); workspace.setOnLongClickListener(this); workspace.setDragController(dragController); workspace.setLauncher(this); deleteZone.setLauncher(this); deleteZone.setDragController(dragController); deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster)); dragController.setDragScoller(workspace); dragController.setDragListener(deleteZone); dragController.setScrollView(mDragLayer); dragController.setMoveTarget(workspace); // The order here is bottom to top. dragController.addDropTarget(workspace); dragController.addDropTarget(deleteZone); } public void previousScreen(View v) { if (!isAllAppsVisible()) { mWorkspace.scrollLeft(); } } public void nextScreen(View v) { if (!isAllAppsVisible()) { mWorkspace.scrollRight(); } } public void launchHotSeat(View v) { if (isAllAppsVisible()) return; int index = -1; if (v.getId() == R.id.hotseat_left) { index = 0; } else if (v.getId() == R.id.hotseat_right) { index = 1; } // reload these every tap; you never know when they might change loadHotseats(); if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) { startActivitySafely( mHotseats[index], "hotseat" ); } } /** * Creates a view representing a shortcut. * * @param info The data structure describing the shortcut. * * @return A View inflated from R.layout.application. */ View createShortcut(ShortcutInfo info) { return createShortcut(R.layout.application, (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); } /** * Creates a view representing a shortcut inflated from the specified resource. * * @param layoutResId The id of the XML layout used to create the shortcut. * @param parent The group the shortcut belongs to. * @param info The data structure describing the shortcut. * * @return A View inflated from layoutResId. */ View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) { TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false); favorite.setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(info.getIcon(mIconCache)), null, null); favorite.setText(info.getTitle(mIconCache)); favorite.setTag(info); favorite.setOnClickListener(this); return favorite; } /** * Add an application shortcut to the workspace. * * @param data The intent describing the application. * @param cellInfo The position on screen where to create the shortcut. */ void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) { cellInfo.screen = mWorkspace.getCurrentScreen(); if (!findSingleSlot(cellInfo)) return; final ShortcutInfo info = mModel.getShortcutInfo(data, context); if (info != null) { info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); info.container = ItemInfo.NO_ID; mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked()); } else { Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data); } } /** * Add a shortcut to the workspace. * * @param data The intent describing the shortcut. * @param cellInfo The position on screen where to create the shortcut. */ private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) { cellInfo.screen = mWorkspace.getCurrentScreen(); if (!findSingleSlot(cellInfo)) return; final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false); if (!mRestoring) { final View view = createShortcut(info); mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked()); } } /** * Remove an item from the desktop * @param info */ void removeDesktopItem(ItemInfo info) { if(info instanceof LauncherAppWidgetInfo) removeAppWidget((LauncherAppWidgetInfo)info); else { if(mDesktopItems!=null) mDesktopItems.remove(info); View view = mWorkspace.getViewForTag(info); if (view != null) ((ViewGroup) view.getParent()).removeView(view); } } public String getPackageNameFromIntent(Intent intent) { String pName = intent.getPackage(); if (pName == null) { ComponentName cName = intent.getComponent(); if (cName != null) pName = cName.getPackageName(); } if (pName == null) { PackageManager mgr = getPackageManager(); ResolveInfo res = mgr.resolveActivity(intent, 0); if (res != null) pName = res.activityInfo.packageName; } if (pName == null) { Uri data = intent.getData(); if ( data != null ) { String host = data.getHost(); PackageManager mgr = getPackageManager(); ProviderInfo packageInfo = mgr.resolveContentProvider(host, 0); if (packageInfo != null) pName = packageInfo.packageName; } } return pName; } public boolean UninstallPackage(String aPackage) { if (aPackage == null || this.getClass().getPackage().getName().equals(aPackage)) return false; Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, Uri.parse("package:"+aPackage)); startActivity(uninstallIntent); return true; } /** * Add a widget to the workspace. * * @param data The intent describing the appWidgetId. * @param cellInfo The position on screen where to create the widget. */ private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) { Bundle extras = data.getExtras(); int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString()); AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); // Calculate the grid spans needed to fit this widget CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen); int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight); // Try finding open space on Launcher screen final int[] xy = mCellCoordinates; if (!findSlot(cellInfo, xy, spans[0], spans[1])) { if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId); return; } // Build Launcher-specific widget info and save to database LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId); launcherInfo.spanX = spans[0]; launcherInfo.spanY = spans[1]; mModel.addItemToDatabase(this, launcherInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP, mWorkspace.getCurrentScreen(), xy[0], xy[1], false); if (!mRestoring) { mDesktopItems.add(launcherInfo); // Perform actual inflation because we're live launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo); launcherInfo.hostView.setTag(launcherInfo); mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1], launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked()); mModel.mAppWidgets.add(launcherInfo); // finish load a widget, send it an intent if(appWidgetInfo!=null) appwidgetReadyBroadcast(appWidgetId, appWidgetInfo.provider); } } public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) { int appWidgetId = launcherInfo.appWidgetId; if(mWorkspace.isWidgetScrollable(appWidgetId)) mWorkspace.unbindWidgetScrollableId(appWidgetId); mDesktopItems.remove(launcherInfo); mModel.mAppWidgets.remove(launcherInfo); launcherInfo.hostView = null; } public LauncherAppWidgetHost getAppWidgetHost() { return mAppWidgetHost; } void closeSystemDialogs() { getWindow().closeAllPanels(); try { dismissDialog(DIALOG_CREATE_SHORTCUT); // Unlock the workspace if the dialog was showing } catch (Exception e) { // An exception is thrown if the dialog is not visible, which is fine } try { dismissDialog(DIALOG_RENAME_FOLDER); // Unlock the workspace if the dialog was showing } catch (Exception e) { // An exception is thrown if the dialog is not visible, which is fine } // Whatever we were doing is hereby canceled. mWaitingForResult = false; } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); // Close the menu if (Intent.ACTION_MAIN.equals(intent.getAction())) { // also will cancel mWaitingForResult. closeSystemDialogs(); boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); boolean allAppsVisible = isAllAppsVisible(); if (!mWorkspace.isDefaultScreenShowing()) { mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible); } closeAllApps(alreadyOnHome && allAppsVisible); final View v = getWindow().peekDecorView(); if (v != null && v.getWindowToken() != null) { InputMethodManager imm = (InputMethodManager)getSystemService( INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } } } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { // Do not call super here mSavedInstanceState = savedInstanceState; } @Override protected void onSaveInstanceState(Bundle outState) { outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen()); final ArrayList<Folder> folders = mWorkspace.getOpenFolders(); if (folders.size() > 0) { final int count = folders.size(); long[] ids = new long[count]; for (int i = 0; i < count; i++) { final FolderInfo info = folders.get(i).getInfo(); ids[i] = info.id; } outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids); } else { super.onSaveInstanceState(outState); } // TODO should not do this if the drawer is currently closing. if (isAllAppsVisible()) { outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true); } if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) { final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen); outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen); outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX); outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY); outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX); outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY); outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX()); outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY()); outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS, layout.getOccupiedCells()); } if (mFolderInfo != null && mWaitingForResult) { outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true); outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id); } } @Override public void onDestroy() { super.onDestroy(); try { mAppWidgetHost.stopListening(); } catch (NullPointerException ex) { Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex); } TextKeyListener.getInstance().release(); mModel.stopLoader(); unbindDesktopItems(); getContentResolver().unregisterContentObserver(mWidgetObserver); dismissPreview(mPreviousView); dismissPreview(mNextView); unregisterReceiver(mCloseSystemDialogsReceiver); mWorkspace.unregisterProvider(); } @Override public void startActivityForResult(Intent intent, int requestCode) { if (requestCode >= 0) mWaitingForResult = true; super.startActivityForResult(intent, requestCode); } @Override public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData, boolean globalSearch) { closeAllApps(true); if (initialQuery == null) { // Use any text typed in the launcher as the initial query initialQuery = getTypedText(); clearTypedText(); } // TODO_BOOMBULER: /* if (appSearchData == null) { appSearchData = new Bundle(); appSearchData.putString(Search.SOURCE, "launcher-search"); }*/ final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(), appSearchData, globalSearch); } @Override public boolean onCreateOptionsMenu(Menu menu) { if (isWorkspaceLocked()) { return false; } super.onCreateOptionsMenu(menu); menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add) .setIcon(android.R.drawable.ic_menu_add) .setAlphabeticShortcut('A'); DefaultAction.getAction(this, DefaultAction.ACTION_MANAGE_APPS).addToMenu(menu, MENU_GROUP_HOMESCREEN); DefaultAction.getAction(this, DefaultAction.ACTION_SHOW_ULNR_SETTINGS).addToMenu(menu, MENU_GROUP_HOMESCREEN); menu.add(MENU_GROUP_DRAWER, MENU_DRAWER_ADD_FOLDER, 0, "Add Folder"); menu.add(MENU_GROUP_HOMESCREEN, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper) .setIcon(android.R.drawable.ic_menu_gallery) .setAlphabeticShortcut('W'); menu.add(MENU_GROUP_HOMESCREEN, MENU_SEARCH, 0, R.string.menu_search) .setIcon(android.R.drawable.ic_search_category_default) .setAlphabeticShortcut(SearchManager.MENU_KEY); DefaultAction.getAction(this, DefaultAction.ACTION_SHOW_NOTIFICATIONS).addToMenu(menu, MENU_GROUP_HOMESCREEN); DefaultAction.getAction(this, DefaultAction.ACTION_SYSTEM_SETTINGS).addToMenu(menu, MENU_GROUP_HOMESCREEN); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); if(mIsWidgetEditMode)return false; // If all apps is animating, don't show the menu, because we don't know // which one to show. if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) { return false; } // Only show the add and wallpaper options when we're not in all apps. boolean visible = !mAllAppsGrid.isOpaque(); menu.setGroupVisible(MENU_GROUP_HOMESCREEN, visible); menu.setGroupVisible(MENU_GROUP_DRAWER, !visible); // Disable add if the workspace is full. if (visible) { mMenuAddInfo = mWorkspace.findAllVacantCells(null); menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid); } return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_ADD: addItems(); return true; case MENU_WALLPAPER_SETTINGS: startWallpaper(); return true; case MENU_SEARCH: onSearchRequested(); return true; case MENU_DRAWER_ADD_FOLDER: requestPickLiveFolder(true); return true; } return super.onOptionsItemSelected(item); } /** * Indicates that we want global search for this activity by setting the globalSearch * argument for {@link #startSearch} to true. */ @Override public boolean onSearchRequested() { startSearch(null, false, null, true); return true; } public boolean isWorkspaceLocked() { return mWorkspaceLoading || mWaitingForResult; } private void addItems() { closeAllApps(true); showAddDialog(mMenuAddInfo); } void addAppWidget(Intent data) { // TODO: catch bad widget exception when sent int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId); if (appWidget.configure != null) { // Launch over to configure widget, if needed Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); intent.setComponent(appWidget.configure); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET); } else { // Otherwise just add it onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data); } } void processShortcut(Intent intent) { // Handle case where user selected "Applications" String applicationName = getResources().getString(R.string.group_applications); String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); if (applicationName != null && applicationName.equals(shortcutName)) { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent); pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application)); startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION); } else { startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT); } } void addLiveFolder(Intent intent, boolean toDrawer) { // Handle case where user selected "Folder" String folderName = getResources().getString(R.string.group_folder); String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); if (folderName != null && folderName.equals(shortcutName)) { if (toDrawer) addFolderToDrawer(); else addFolder(); } else { startActivityForResultSafely(intent, toDrawer ? REQUEST_CREATE_LIVE_FOLDER_DRAWER : REQUEST_CREATE_LIVE_FOLDER); } } void addFolder() { UserFolderInfo folderInfo = new UserFolderInfo(); folderInfo.setTitle(getText(R.string.folder_name)); CellLayout.CellInfo cellInfo = mAddItemCellInfo; cellInfo.screen = mWorkspace.getCurrentScreen(); if (!findSingleSlot(cellInfo)) return; // Update the model mModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP, mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false); sFolders.put(folderInfo.id, folderInfo); // Create the view FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo); mWorkspace.addInCurrentScreen(newFolder, cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked()); } void removeFolder(FolderInfo folder) { sFolders.remove(folder.id); } private void completeAddLiveFolderDrawer(Intent data) { final LiveFolderInfo info = addLiveFolder(this, data, null, false); if (!mRestoring) { ArrayList<IconItemInfo> list = new ArrayList<IconItemInfo>(); list.add(info); getAllAppsView().addApps(list); } } private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) { cellInfo.screen = mWorkspace.getCurrentScreen(); if (!findSingleSlot(cellInfo)) return; final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false); if (!mRestoring) { final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this, (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked()); } } LiveFolderInfo addLiveFolder(Context context, Intent data, CellLayout.CellInfo cellInfo, boolean notify) { Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT); String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME); Drawable icon = null; Intent.ShortcutIconResource iconResource = null; Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON); if (extra != null && extra instanceof Intent.ShortcutIconResource) { try { iconResource = (Intent.ShortcutIconResource) extra; final PackageManager packageManager = context.getPackageManager(); Resources resources = packageManager.getResourcesForApplication( iconResource.packageName); final int id = resources.getIdentifier(iconResource.resourceName, null, null); icon = resources.getDrawable(id); } catch (Exception e) { Log.w(TAG, "Could not load live folder icon: " + extra); } } if (icon == null) { icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder); } final LiveFolderInfo info = new LiveFolderInfo(); info.setIcon(Utilities.createIconBitmap(icon, context)); info.setTitle(name); info.uri = data.getData(); info.baseIntent = baseIntent; info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, LiveFolders.DISPLAY_MODE_GRID); if (cellInfo != null) { mModel.addItemToDatabase(context, info, Favorites.CONTAINER_DESKTOP, cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); sFolders.put(info.id, info); } else { mModel.addItemToDatabase(context, info, Favorites.CONTAINER_DRAWER, ItemInfo.NO_ID, 0, 0, notify); } return info; } private boolean findSingleSlot(CellLayout.CellInfo cellInfo) { final int[] xy = new int[2]; if (findSlot(cellInfo, xy, 1, 1)) { cellInfo.cellX = xy[0]; cellInfo.cellY = xy[1]; return true; } return false; } private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) { if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { boolean[] occupied = mSavedState != null ? mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null; cellInfo = mWorkspace.findAllVacantCells(occupied); if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show(); return false; } } return true; } private void addFolderToDrawer() { UserFolderInfo folderInfo = new UserFolderInfo(); folderInfo.setTitle(getText(R.string.folder_name)); mModel.addItemToDatabase(this, folderInfo, Favorites.CONTAINER_DRAWER, -1, 0, 0, false); ArrayList<IconItemInfo> list = new ArrayList<IconItemInfo>(); list.add(folderInfo); mAllAppsGrid.addApps(list); } private void startWallpaper() { closeAllApps(true); final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER); Intent chooser = Intent.createChooser(pickWallpaper, getText(R.string.chooser_wallpaper)); // NOTE: Adds a configure option to the chooser if the wallpaper supports it // Removed in Eclair MR1 // WallpaperManager wm = (WallpaperManager) // getSystemService(Context.WALLPAPER_SERVICE); // WallpaperInfo wi = wm.getWallpaperInfo(); // if (wi != null && wi.getSettingsActivity() != null) { // LabeledIntent li = new LabeledIntent(getPackageName(), // R.string.configure_wallpaper, 0); // li.setClassName(wi.getPackageName(), wi.getSettingsActivity()); // chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li }); // } startActivityForResult(chooser, REQUEST_PICK_WALLPAPER); } /** * Registers various content observers. The current implementation registers * only a favorites observer to keep track of the favorites applications. */ private void registerContentObservers() { ContentResolver resolver = getContentResolver(); resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI, true, mWidgetObserver); } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { switch (event.getKeyCode()) { case KeyEvent.KEYCODE_HOME: return true; } } else if (event.getAction() == KeyEvent.ACTION_UP) { switch (event.getKeyCode()) { case KeyEvent.KEYCODE_HOME: return true; case KeyEvent.KEYCODE_BACK: { if (!event.isCanceled()) { if(mIsWidgetEditMode){ stopWidgetEdit(); } } onBackPressed(); return true; } } } return super.dispatchKeyEvent(event); } @Override public void onBackPressed() { if (isAllAppsVisible()) { closeAllApps(true); } else { closeFolder(); } dismissPreview(mPreviousView); dismissPreview(mNextView); } private void closeFolder() { Folder folder = mWorkspace.getOpenFolder(); if (folder != null) { closeFolder(folder); } } void closeFolder(Folder folder) { folder.getInfo().opened = false; ViewGroup parent = (ViewGroup) folder.getParent(); if (parent != null) { parent.removeView(folder); if (folder instanceof DropTarget) { // Live folders aren't DropTargets. mDragController.removeDropTarget((DropTarget)folder); } } folder.onClose(); } /** * Re-listen when widgets are reset. */ private void onAppWidgetReset() { mAppWidgetHost.startListening(); } /** * Go through the and disconnect any of the callbacks in the drawables and the views or we * leak the previous Home screen on orientation change. */ private void unbindDesktopItems() { for (ItemInfo item: mDesktopItems) { item.unbind(); } } /** * Launches the intent referred by the clicked shortcut. * * @param v The view representing the clicked shortcut. */ public void onClick(View v) { Object tag = v.getTag(); if (tag instanceof ShortcutInfo) { // Open shortcut final Intent intent = ((ShortcutInfo) tag).intent; int[] pos = new int[2]; v.getLocationOnScreen(pos); intent.setSourceBounds(new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight())); startActivitySafely(intent, tag); } else if (tag instanceof FolderInfo) { openFolder((FolderInfo) tag); } else if (v == mHandleView) { if (isAllAppsVisible()) { closeAllApps(true); } else { showAllApps(true); } } } void startActivitySafely(Intent intent, Object tag) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { startActivity(intent); } catch (ActivityNotFoundException e) { Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e); } catch (SecurityException e) { Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); Log.e(TAG, "Launcher does not have the permission to launch " + intent + ". Make sure to create a MAIN intent-filter for the corresponding activity " + "or use the exported attribute for this activity. " + "tag="+ tag + " intent=" + intent, e); } if(mAppDB.incrementLaunchCounter(intent)) { mAllAppsGrid.sort(); } } void startActivityForResultSafely(Intent intent, int requestCode) { try { startActivityForResult(intent, requestCode); } catch (ActivityNotFoundException e) { Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); } catch (SecurityException e) { Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); Log.e(TAG, "Launcher does not have the permission to launch " + intent + ". Make sure to create a MAIN intent-filter for the corresponding activity " + "or use the exported attribute for this activity.", e); } } public void openFolder(FolderInfo folderInfo) { if (!folderInfo.opened) { // Close any open folder closeFolder(); // Open the requested folder doOpenFolder(folderInfo); } else { // Find the open folder... Folder openFolder = mWorkspace.getFolderForTag(folderInfo); int folderScreen; if (openFolder != null) { folderScreen = mWorkspace.getScreenForView(openFolder); // .. and close it closeFolder(openFolder); if (folderScreen != mWorkspace.getCurrentScreen()) { // Close any folder open on the current screen closeFolder(); // Pull the folder onto this screen openFolder(folderInfo); } } } } /** * Opens the user fodler described by the specified tag. The opening of the folder * is animated relative to the specified View. If the View is null, no animation * is played. * * @param folderInfo The FolderInfo describing the folder to open. */ private void doOpenFolder(FolderInfo folderInfo) { Folder openFolder; if (folderInfo instanceof UserFolderInfo) { openFolder = UserFolder.fromXml(this); } else if (folderInfo instanceof LiveFolderInfo) { openFolder = cn.walcl.ulauncher.LiveFolder.fromXml(this, folderInfo); } else { return; } if (isAllAppsVisible()) closeAllApps(false); openFolder.setDragController(mDragController); openFolder.setLauncher(this); openFolder.bind(folderInfo); folderInfo.opened = true; int screen = folderInfo.screen; if (screen < 0) screen = mWorkspace.getCurrentScreen(); mWorkspace.addInScreen(openFolder, screen, 0, 0, 4, 4); openFolder.onOpen(); } public boolean onLongClick(View v) { switch (v.getId()) { case R.id.previous_screen: if (!isAllAppsVisible()) { mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); showPreviews(v); } return true; case R.id.next_screen: if (!isAllAppsVisible()) { mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); showPreviews(v); } return true; case R.id.all_apps_button: if (!isAllAppsVisible()) { mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); showPreviews(v); } return true; } if (isWorkspaceLocked()) { return false; } if (!(v instanceof CellLayout)) { v = (View) v.getParent(); } CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag(); // This happens when long clicking an item with the dpad/trackball if (cellInfo == null) { return true; } if (mWorkspace.allowLongPress()) { if (cellInfo.cell == null) { if (cellInfo.valid) { // User long pressed on empty space mWorkspace.setAllowLongPress(false); mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); showAddDialog(cellInfo); } } else { if (!(cellInfo.cell instanceof Folder)) { // User long pressed on an item mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); mWorkspace.startDrag(cellInfo); } } } return true; } @SuppressWarnings({"unchecked"}) private void dismissPreview(final View v) { final PopupWindow window = (PopupWindow) v.getTag(); if (window != null) { window.setOnDismissListener(new PopupWindow.OnDismissListener() { public void onDismiss() { ViewGroup group = (ViewGroup) v.getTag(R.id.workspace); int count = group.getChildCount(); for (int i = 0; i < count; i++) { ((ImageView) group.getChildAt(i)).setImageDrawable(null); } ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon); for (Bitmap bitmap : bitmaps) bitmap.recycle(); v.setTag(R.id.workspace, null); v.setTag(R.id.icon, null); window.setOnDismissListener(null); } }); window.dismiss(); } v.setTag(null); } private void showPreviews(View anchor) { showPreviews(anchor, 0, mWorkspace.getChildCount()); } private void showPreviews(final View anchor, int start, int end) { final Resources resources = getResources(); final Workspace workspace = mWorkspace; CellLayout cell = ((CellLayout) workspace.getChildAt(start)); float max = workspace.getChildCount(); final Rect r = new Rect(); resources.getDrawable(R.drawable.preview_background).getPadding(r); int extraW = (int) ((r.left + r.right) * max); int extraH = r.top + r.bottom; int aW = cell.getWidth() - extraW; float w = aW / max; int width = cell.getWidth(); int height = cell.getHeight(); int x = cell.getLeftPadding(); int y = cell.getTopPadding(); width -= (x + cell.getRightPadding()); height -= (y + cell.getBottomPadding()); float scale = w / width; int count = end - start; final float sWidth = width * scale; float sHeight = height * scale; LinearLayout preview = new LinearLayout(this); PreviewTouchHandler handler = new PreviewTouchHandler(anchor); ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count); for (int i = start; i < end; i++) { ImageView image = new ImageView(this); cell = (CellLayout) workspace.getChildAt(i); final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight, Bitmap.Config.ARGB_8888); final Canvas c = new Canvas(bitmap); c.scale(scale, scale); c.translate(-cell.getLeftPadding(), -cell.getTopPadding()); cell.dispatchDraw(c); image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background)); image.setImageBitmap(bitmap); image.setTag(i); image.setOnClickListener(handler); image.setOnFocusChangeListener(handler); image.setFocusable(true); if (i == mWorkspace.getCurrentScreen()) image.requestFocus(); preview.addView(image, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); bitmaps.add(bitmap); } final PopupWindow p = new PopupWindow(this); p.setContentView(preview); p.setWidth((int) (sWidth * count + extraW)); p.setHeight((int) (sHeight + extraH)); p.setAnimationStyle(R.style.AnimationPreview); p.setOutsideTouchable(true); p.setFocusable(true); p.setBackgroundDrawable(new ColorDrawable(0)); p.showAsDropDown(anchor, 0, 0); p.setOnDismissListener(new PopupWindow.OnDismissListener() { public void onDismiss() { dismissPreview(anchor); } }); anchor.setTag(p); anchor.setTag(R.id.workspace, preview); anchor.setTag(R.id.icon, bitmaps); } class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener { private final View mAnchor; public PreviewTouchHandler(View anchor) { mAnchor = anchor; } public void onClick(View v) { mWorkspace.snapToScreen((Integer) v.getTag()); v.post(this); } public void run() { dismissPreview(mAnchor); } public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { mWorkspace.snapToScreen((Integer) v.getTag()); } } } Workspace getWorkspace() { return mWorkspace; } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_CREATE_SHORTCUT: return new CreateShortcut().createDialog(); case DIALOG_RENAME_FOLDER: return new RenameFolder().createDialog(); } return super.onCreateDialog(id); } @Override protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case DIALOG_CREATE_SHORTCUT: break; case DIALOG_RENAME_FOLDER: if (mFolderInfo != null) { EditText input = (EditText) dialog.findViewById(R.id.folder_name); final CharSequence text = mFolderInfo.getTitle(mIconCache); input.setText(text); input.setSelection(0, text.length()); } break; } } void showRenameDialog(FolderInfo info) { mFolderInfo = info; mWaitingForResult = true; showDialog(DIALOG_RENAME_FOLDER); } private void showAddDialog(CellLayout.CellInfo cellInfo) { mAddItemCellInfo = cellInfo; mWaitingForResult = true; showDialog(DIALOG_CREATE_SHORTCUT); } private void pickShortcut() { Bundle bundle = new Bundle(); ArrayList<String> shortcutNames = new ArrayList<String>(); shortcutNames.add(getString(R.string.group_applications)); bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>(); shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, R.drawable.ic_launcher_application)); bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT)); pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut)); pickIntent.putExtras(bundle); startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT); } private class RenameFolder { private EditText mInput; Dialog createDialog() { mWaitingForResult = true; final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null); mInput = (EditText) layout.findViewById(R.id.folder_name); AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); builder.setIcon(0); builder.setTitle(getString(R.string.rename_folder_title)); builder.setCancelable(true); builder.setOnCancelListener(new Dialog.OnCancelListener() { public void onCancel(DialogInterface dialog) { cleanup(); } }); builder.setNegativeButton(getString(R.string.cancel_action), new Dialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { cleanup(); } } ); builder.setPositiveButton(getString(R.string.rename_action), new Dialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { changeFolderName(); } } ); builder.setView(layout); final AlertDialog dialog = builder.create(); return dialog; } private void changeFolderName() { final String name = mInput.getText().toString(); if (!TextUtils.isEmpty(name)) { // Make sure we have the right folder info if (sFolders.containsKey(mFolderInfo.id)) mFolderInfo = sFolders.get(mFolderInfo.id); mFolderInfo.setTitle(name); LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo); if (mWorkspaceLoading) { lockAllApps(); mModel.startLoader(Launcher.this, false); } else { final FolderIcon folderIcon = (FolderIcon) mWorkspace.getViewForTag(mFolderInfo); if (folderIcon != null) { folderIcon.setText(name); getWorkspace().requestLayout(); } else { lockAllApps(); mWorkspaceLoading = true; mModel.startLoader(Launcher.this, false); } } } cleanup(); } private void cleanup() { dismissDialog(DIALOG_RENAME_FOLDER); mWaitingForResult = false; mFolderInfo = null; } } // Now a part of LauncherModel.Callbacks. Used to reorder loading steps. public boolean isAllAppsVisible() { return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false; } // AllAppsView.Watcher public void zoomed(float zoom) { if (zoom == 1.0f) { mWorkspace.setVisibility(View.GONE); } } private void requestPickLiveFolder(boolean toDrawer) { // Insert extra item to handle inserting folder Bundle bundle = new Bundle(); ArrayList<String> shortcutNames = new ArrayList<String>(); shortcutNames.add(getString(R.string.group_folder)); bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>(); shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, R.drawable.ic_launcher_folder)); bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER)); pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_live_folder)); pickIntent.putExtras(bundle); startActivityForResult(pickIntent, toDrawer ? REQUEST_PICK_LIVE_FOLDER_DRAWER : REQUEST_PICK_LIVE_FOLDER); } public void showAllApps(boolean animated) { mAllAppsGrid.zoom(1.0f, animated); ((View) mAllAppsGrid).setFocusable(true); ((View) mAllAppsGrid).requestFocus(); // TODO: fade these two too mDeleteZone.setVisibility(View.GONE); } /** * Things to test when changing this code. * - Home from workspace * - from center screen * - from other screens * - Home from all apps * - from center screen * - from other screens * - Back from all apps * - from center screen * - from other screens * - Launch app from workspace and quit * - with back * - with home * - Launch app from all apps and quit * - with back * - with home * - Go to a screen that's not the default, then all * apps, and launch and app, and go back * - with back * -with home * - On workspace, long press power and go back * - with back * - with home * - On all apps, long press power and go back * - with back * - with home * - On workspace, power off * - On all apps, power off * - Launch an app and turn off the screen while in that app * - Go back with home key * - Go back with back key TODO: make this not go to workspace * - From all apps * - From workspace * - Enter and exit car mode (becuase it causes an extra configuration changed) * - From all apps * - From the center workspace * - From another workspace */ public void closeAllApps(boolean animated) { if (mAllAppsGrid.isVisible()) { mWorkspace.setVisibility(View.VISIBLE); mAllAppsGrid.zoom(0.0f, animated); ((View)mAllAppsGrid).setFocusable(false); mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); } } void lockAllApps() { // TODO } void unlockAllApps() { // TODO } /** * Displays the shortcut creation dialog and launches, if necessary, the * appropriate activity. */ private class CreateShortcut implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener, DialogInterface.OnDismissListener { private AddAdapter mAdapter; Dialog createDialog() { mWaitingForResult = true; mAdapter = new AddAdapter(Launcher.this); final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); builder.setTitle(getString(R.string.menu_item_add_item)); builder.setAdapter(mAdapter, this); builder.setInverseBackgroundForced(true); AlertDialog dialog = builder.create(); dialog.setOnCancelListener(this); dialog.setOnDismissListener(this); return dialog; } public void onCancel(DialogInterface dialog) { mWaitingForResult = false; cleanup(); } public void onDismiss(DialogInterface dialog) { } private void cleanup() { try { dismissDialog(DIALOG_CREATE_SHORTCUT); } catch (Exception e) { // An exception is thrown if the dialog is not visible, which is fine } } /** * Handle the action clicked in the "Add to home" dialog. */ public void onClick(DialogInterface dialog, int which) { getResources(); cleanup(); switch (which) { case AddAdapter.ITEM_SHORTCUT: { // Insert extra item to handle picking application pickShortcut(); break; } case AddAdapter.ITEM_APPWIDGET: { int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId(); Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK); pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); // start the pick activity startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET); break; } case AddAdapter.ITEM_LIVE_FOLDER: { Launcher.this.requestPickLiveFolder(false); break; } case AddAdapter.ITEM_ANYCUT: { Intent anycutIntent=new Intent(); anycutIntent.setClass(Launcher.this, CustomShirtcutActivity.class); startActivityForResult(anycutIntent, REQUEST_PICK_ANYCUT); break; } case AddAdapter.ITEM_WALLPAPER: { startWallpaper(); break; } } } } /** * Receives notifications when applications are added/removed. */ private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { closeSystemDialogs(); String reason = intent.getStringExtra("reason"); if (!"homekey".equals(reason)) { boolean animate = true; if (mPaused || "lock".equals(reason)) { animate = false; } closeAllApps(animate); } } } /** * Receives notifications whenever the appwidgets are reset. */ private class AppWidgetResetObserver extends ContentObserver { public AppWidgetResetObserver() { super(new Handler()); } @Override public void onChange(boolean selfChange) { onAppWidgetReset(); } } /** * If the activity is currently paused, signal that we need to re-run the loader * in onResume. * * This needs to be called from incoming places where resources might have been loaded * while we are paused. That is becaues the Configuration might be wrong * when we're not running, and if it comes back to what it was when we * were paused, we are not restarted. * * Implementation of the method from LauncherModel.Callbacks. * * @return true if we are currently paused. The caller might be able to * skip some work in that case since we will come back again. */ public boolean setLoadOnResume() { if (mPaused) { Log.i(TAG, "setLoadOnResume"); mOnResumeNeedsLoad = true; return true; } else { return false; } } /** * Implementation of the method from LauncherModel.Callbacks. */ public int getCurrentWorkspaceScreen() { if (mWorkspace != null) { return mWorkspace.getCurrentScreen(); } else { return SCREEN_COUNT / 2; } } /** * Refreshes the shortcuts shown on the workspace. * * Implementation of the method from LauncherModel.Callbacks. */ public void startBinding() { final Workspace workspace = mWorkspace; int count = workspace.getChildCount(); for (int i = 0; i < count; i++) { // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate(). ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout(); } } /** * Bind the items start-end from the list. * * Implementation of the method from LauncherModel.Callbacks. */ public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) { setLoadOnResume(); final Workspace workspace = mWorkspace; for (int i=start; i<end; i++) { final ItemInfo item = shortcuts.get(i); mDesktopItems.add(item); switch (item.itemType) { case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: final View shortcut = createShortcut((ShortcutInfo)item); workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1, false); break; case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), (UserFolderInfo) item); workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1, false); break; case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: final FolderIcon newLiveFolder = LiveFolderIcon.fromXml( R.layout.live_folder_icon, this, (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), (LiveFolderInfo) item); workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1, false); break; } } workspace.requestLayout(); } /** * Implementation of the method from LauncherModel.Callbacks. */ public void bindFolders(HashMap<Long, FolderInfo> folders) { setLoadOnResume(); sFolders.clear(); sFolders.putAll(folders); } /** * Add the views for a widget to the workspace. * * Implementation of the method from LauncherModel.Callbacks. */ public void bindAppWidget(LauncherAppWidgetInfo item) { setLoadOnResume(); final Workspace workspace = mWorkspace; final int appWidgetId = item.appWidgetId; final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); item.hostView.setAppWidget(appWidgetId, appWidgetInfo); item.hostView.setTag(item); workspace.addInScreen(item.hostView, item.screen, item.cellX, item.cellY, item.spanX, item.spanY, false); workspace.requestLayout(); // finish load a widget, send it an intent if (appWidgetInfo != null) appwidgetReadyBroadcast(appWidgetId, appWidgetInfo.provider); mDesktopItems.add(item); } private void appwidgetReadyBroadcast(int appWidgetId, ComponentName cname) { Intent ready = new Intent(LauncherIntent.Action.ACTION_READY).putExtra( LauncherIntent.Extra.EXTRA_APPWIDGET_ID, appWidgetId).putExtra( AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId).putExtra( LauncherIntent.Extra.EXTRA_API_VERSION, LauncherMetadata.CurrentAPIVersion). setComponent(cname); sendBroadcast(ready); } /** * Callback saying that there aren't any more items to bind. * * Implementation of the method from LauncherModel.Callbacks. */ public void finishBindingItems() { setLoadOnResume(); if (mSavedState != null) { if (!mWorkspace.hasFocus()) { mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); } final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS); if (userFolders != null) { for (long folderId : userFolders) { final FolderInfo info = sFolders.get(folderId); if (info != null) { doOpenFolder(info); } } final Folder openFolder = mWorkspace.getOpenFolder(); if (openFolder != null) { openFolder.requestFocus(); } } mSavedState = null; } if (mSavedInstanceState != null) { //ADW: sometimes on rotating the phone, // some widgets fail to restore its states.... so... damn. // In more detail: The scrollable widget fail. // Would be nice to have a good workaround here! try{ super.onRestoreInstanceState(mSavedInstanceState); }catch(Exception e){} mSavedInstanceState = null; } mWorkspaceLoading = false; } /** * Add the icons for all apps. * * Implementation of the method from LauncherModel.Callbacks. */ public void bindAllApplications(ArrayList<ShortcutInfo> apps, ArrayList<IconItemInfo> otherItems) { mAllAppsGrid.setApps(apps); mAllAppsGrid.addApps(otherItems); } /** * A package was installed. * * Implementation of the method from LauncherModel.Callbacks. */ public void bindAppsAdded(ArrayList<? extends IconItemInfo> apps) { setLoadOnResume(); removeDialog(DIALOG_CREATE_SHORTCUT); mAllAppsGrid.addApps(apps); } /** * A package was updated. * * Implementation of the method from LauncherModel.Callbacks. */ public void bindAppsUpdated(ArrayList<ShortcutInfo> apps) { setLoadOnResume(); removeDialog(DIALOG_CREATE_SHORTCUT); mWorkspace.updateShortcuts(apps); mAllAppsGrid.updateApps(apps); } /** * A package was uninstalled. * * Implementation of the method from LauncherModel.Callbacks. */ public void bindAppsRemoved(ArrayList<ShortcutInfo> apps, boolean permanent) { removeDialog(DIALOG_CREATE_SHORTCUT); if (permanent) { mWorkspace.removeItems(apps); } mAllAppsGrid.removeApps(apps); } public void showActions(ItemInfo info, View view, PopupWindow.OnDismissListener onDismissListener) { if (info == null || view == null) return; List<EditAction> actions = info.getAvailableActions(view, this); if (actions.size() <= 0) return; final View finalview = view; final ItemInfo finalInfo = info; int[] xy = new int[2]; //fills the array with the computed coordinates view.getLocationInWindow(xy); new Rect(xy[0], xy[1], xy[0]+view.getWidth(), xy[1]+view.getHeight()); //a new QuickActionWindow object final QuickAction qa = new QuickAction(view); view.setTag(cn.walcl.ulauncher.R.id.TAG_PREVIEW, qa); if (onDismissListener != null) { qa.setOnDismissListener(onDismissListener); } //adds an item to the badge and defines the quick action to be triggered //when the item is clicked on for(EditAction action : actions) { final EditAction finalaction = action; Drawable icon; CharSequence title; if ( action.getIconResourceId() == 0 ) { icon = action.getIconDrawable(); } else { icon = getResources().getDrawable(action.getIconResourceId()); } if( action.getTitleResourceId() == 0 ) { title = action.getTitleString(); } else { title = getResources().getString(action.getTitleResourceId()); } ActionItem ai = new ActionItem(icon); if (title != null) ai.setTitle(title.toString()); ai.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { qa.dismiss(); finalInfo.executeAction(finalaction, finalview, Launcher.this); } }); qa.addActionItem(ai); } //shows the quick action window on the screen qa.show(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mWorkspaceLoading = true; final int currScreen=mWorkspace.getCurrentScreen(); checkForLocaleChange(); setWallpaperDimension(); final int count=mWorkspace.getChildCount(); //get icons properties final Resources r=getResources(); final int margintop=r.getDimensionPixelSize(R.dimen.icon_layout_marginTop); final int marginbottom=r.getDimensionPixelSize(R.dimen.icon_layout_marginBottom); final int marginleft=r.getDimensionPixelSize(R.dimen.icon_layout_marginLeft); final int marginright=r.getDimensionPixelSize(R.dimen.icon_layout_marginRight); final int paddingtop=r.getDimensionPixelSize(R.dimen.icon_paddingTop); final int paddingbottom=r.getDimensionPixelSize(R.dimen.icon_paddingBottom); final int paddingleft=r.getDimensionPixelSize(R.dimen.icon_paddingLeft); final int paddingright=r.getDimensionPixelSize(R.dimen.icon_paddingRight); final int drawablePadding=r.getDimensionPixelSize(R.dimen.icon_drawablePadding); for(int i=0;i<count;i++){ CellLayout screen= (CellLayout) mWorkspace.getChildAt(i); for(int j=0;j<screen.getChildCount();j++){ if(screen.getChildAt(j) instanceof BubbleTextView){ final BubbleTextView v= (BubbleTextView) screen.getChildAt(j); CellLayout.LayoutParams lp= (CellLayout.LayoutParams) v.getLayoutParams(); lp.topMargin=margintop; lp.bottomMargin=marginbottom; lp.leftMargin=marginleft; lp.rightMargin=marginright; v.setLayoutParams(lp); v.setPadding(paddingleft,paddingtop,paddingright,paddingbottom); v.setCompoundDrawablePadding(drawablePadding); } } screen.reMeasure(this); } //setContentView(R.layout.launcher); //setupViews(); //mModel.startLoader(this, false); mAppWidgetHost.stopListening(); for(LauncherAppWidgetInfo w:mModel.mAppWidgets){ final int appWidgetId = w.appWidgetId; final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); if(mWorkspace.isWidgetScrollable(appWidgetId)) mWorkspace.unbindWidgetScrollableId(appWidgetId); CellLayout screen= (CellLayout) mWorkspace.getChildAt(w.screen); screen.removeViewInLayout(w.hostView); w.unbind(); w.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); w.hostView.setAppWidget(appWidgetId, appWidgetInfo); w.hostView.setTag(w); mWorkspace.addInScreen(w.hostView, w.screen, w.cellX, w.cellY, w.spanX, w.spanY, false); } mAppWidgetHost.startListening(); final Workspace workspace = mWorkspace; mWorkspace.setAfterLayoutListener(new Runnable() { @Override public void run() { if (!workspace.isLayoutRequested()) { workspace.setCurrentScreen(currScreen); for(LauncherAppWidgetInfo w:mModel.mAppWidgets){ if(w.hostView != null) { AppWidgetProviderInfo info = w.hostView.getAppWidgetInfo(); if (info != null && info.provider != null) { appwidgetReadyBroadcast(w.appWidgetId, info.provider); } } } workspace.setAfterLayoutListener(null); mWorkspaceLoading = false; } } }); workspace.requestLayout(); } protected void editWidget(final View widget){ if(mWorkspace!=null){ mIsWidgetEditMode=true; final CellLayout screen=(CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()); if(screen!=null){ mEditingAppWidget = (LauncherAppWidgetInfo) widget.getTag(); final long itemId = mEditingAppWidget.id; final Intent motosize = new Intent("com.motorola.blur.home.ACTION_SET_WIDGET_SIZE"); final int appWidgetId = ((AppWidgetHostView)widget).getAppWidgetId(); final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); if(appWidgetInfo!=null){ motosize.setComponent(appWidgetInfo.provider); } motosize.putExtra("appWidgetId",appWidgetId ); motosize.putExtra("com.motorola.blur.home.EXTRA_NEW_WIDGET", true); final int minw=(mWorkspace.getWidth()-screen.getLeftPadding()-screen.getRightPadding())/screen.getCountX(); final int minh=(mWorkspace.getHeight()-screen.getBottomPadding()-screen.getTopPadding())/screen.getCountY(); mScreensEditor=new ResizeViewHandler(this); // Create a default HightlightView if we found no face in the picture. int width = (mEditingAppWidget.spanX*minw); int height = (mEditingAppWidget.spanY*minh); final Rect screenRect = new Rect(0, 0, mWorkspace.getWidth()-screen.getRightPadding(), mWorkspace.getHeight()-screen.getBottomPadding()); final int x=mEditingAppWidget.cellX*minw; final int y=mEditingAppWidget.cellY*minh; final int[]spans=new int[]{1,1}; final int[] position=new int[]{1,1}; final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) widget.getLayoutParams(); RectF widgetRect = new RectF(x,y, x + width, y + height); mScreensEditor.setup(null, screenRect, widgetRect, false,false,minw-10,minh-10); mDragLayer.addView(mScreensEditor); mScreensEditor.setOnValidateSizingRect(new ResizeViewHandler.OnSizeChangedListener() { @Override public void onTrigger(RectF r) { if(r!=null){ final float left = Math.round(r.left/minw) * minw; final float top = Math.round(r.top/minh) * minh; final float right = left + (Math.max(Math.round(r.width()/(minw)),1) * minw); final float bottom = top + (Math.max(Math.round(r.height()/(minh)),1) * minh); r.set(left, top, right, bottom); } } }); final Rect checkRect=new Rect(); (mScreensEditor).setOnSizeChangedListener(new ResizeViewHandler.OnSizeChangedListener() { @Override public void onTrigger(RectF r) { int[]tmpspans={ Math.max(Math.round(r.width()/(minw)),1), Math.max(Math.round(r.height()/(minh)),1) }; int[] tmpposition={ Math.round(r.left/minw), Math.round(r.top/minh) }; checkRect.set(tmpposition[0],tmpposition[1],tmpposition[0]+tmpspans[0],tmpposition[1]+tmpspans[1]); boolean ocupada=ocuppiedArea(screen.getScreen(), itemId,checkRect); if(!ocupada){ (mScreensEditor).setColliding(false); }else{ (mScreensEditor).setColliding(true); } if(tmpposition[0]!=position[0] ||tmpposition[1]!=position[1]|| tmpspans[0]!=spans[0]||tmpspans[1]!=spans[1]){ if(!ocupada){ position[0]=tmpposition[0]; position[1]=tmpposition[1]; spans[0]=tmpspans[0]; spans[1]=tmpspans[1]; lp.cellX = position[0]; lp.cellY = position[1]; lp.cellHSpan = spans[0]; lp.cellVSpan = spans[1]; widget.setLayoutParams(lp); mEditingAppWidget.cellX=lp.cellX; mEditingAppWidget.cellY=lp.cellY; mEditingAppWidget.spanX=lp.cellHSpan; mEditingAppWidget.spanY=lp.cellVSpan; widget.setTag(mEditingAppWidget); //send the broadcast motosize.putExtra("spanX", spans[0]); motosize.putExtra("spanY", spans[1]); Launcher.this.sendBroadcast(motosize); Log.d("RESIZEHANDLER","sent resize broadcast"); } } } }); } } } private void stopWidgetEdit(){ mIsWidgetEditMode=false; if(mEditingAppWidget!=null){ LauncherModel.resizeItemInDatabase(this, mEditingAppWidget, LauncherSettings.Favorites.CONTAINER_DESKTOP, mEditingAppWidget.screen, mEditingAppWidget.cellX, mEditingAppWidget.cellY, mEditingAppWidget.spanX, mEditingAppWidget.spanY); mEditingAppWidget=null; } //Remove the resizehandler view if(mScreensEditor!=null){ mDragLayer.removeView(mScreensEditor); mScreensEditor=null; } } boolean ocuppiedArea(int screen,long ignoreItemId,Rect rect){ final ArrayList<ItemInfo>desktopItems= mDesktopItems; Rect r=new Rect(); for (ItemInfo it : desktopItems) { if(it.screen==screen && it.id != ignoreItemId){ r.set(it.cellX,it.cellY,it.cellX+it.spanX,it.cellY+it.spanY); if(rect.intersect(r)){ return true; } } } return false; } }