Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c0f34c90 authored by Danny Baumann's avatar Danny Baumann Committed by Gerrit Code Review
Browse files

Clean up lockscreen target configuration code (1/2)

Get rid of a lot of duplicated code.

Change-Id: I80fb0b0cd9b5f7d79a5760f8fa6ae338d9ca7205
parent 814a8ff2
Loading
Loading
Loading
Loading
+170 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 The CyanogenMod 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 com.android.internal.util.cm;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.InsetDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.Log;

import com.android.internal.widget.multiwaveview.GlowPadView;
import com.android.internal.widget.multiwaveview.TargetDrawable;

import java.io.File;

public final class LockscreenTargetUtils {
    private static final String TAG = "LockscreenTargetUtils";

    private LockscreenTargetUtils() {
    }

    public static boolean isScreenLarge(Context context) {
        final int screenSize = context.getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK;
        boolean isScreenLarge = screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE ||
                screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
        return isScreenLarge;
    }

    public static int getMaxTargets(Context context) {
        if (isScreenLarge(context)) {
            return GlowPadView.MAX_TABLET_TARGETS;
        }

        return GlowPadView.MAX_PHONE_TARGETS;
    }

    public static int getTargetOffset(Context context) {
        boolean isLandscape = context.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE;
        return isLandscape && !isScreenLarge(context) ? 2 : 0;
    }

    /**
     * Create a layered drawable
     * @param back - Background image to use when target is active
     * @param front - Front image to use for target
     * @param inset - Target inset padding
     * @param frontBlank - Whether the front image for active target should be blank
     * @return StateListDrawable
     */
    public static StateListDrawable getLayeredDrawable(Context context,
            Drawable back, Drawable front, int inset, boolean frontBlank) {
        final Resources res = context.getResources();
        InsetDrawable[] inactivelayer = new InsetDrawable[2];
        InsetDrawable[] activelayer = new InsetDrawable[2];

        inactivelayer[0] = new InsetDrawable(res.getDrawable(
                    com.android.internal.R.drawable.ic_lockscreen_lock_pressed), 0, 0, 0, 0);
        inactivelayer[1] = new InsetDrawable(front, inset, inset, inset, inset);

        activelayer[0] = new InsetDrawable(back, 0, 0, 0, 0);
        activelayer[1] = new InsetDrawable(
                frontBlank ? res.getDrawable(android.R.color.transparent) : front,
                inset, inset, inset, inset);

        LayerDrawable inactiveLayerDrawable = new LayerDrawable(inactivelayer);
        inactiveLayerDrawable.setId(0, 0);
        inactiveLayerDrawable.setId(1, 1);

        LayerDrawable activeLayerDrawable = new LayerDrawable(activelayer);
        activeLayerDrawable.setId(0, 0);
        activeLayerDrawable.setId(1, 1);

        StateListDrawable states = new StateListDrawable();
        states.addState(TargetDrawable.STATE_INACTIVE, inactiveLayerDrawable);
        states.addState(TargetDrawable.STATE_ACTIVE, activeLayerDrawable);
        states.addState(TargetDrawable.STATE_FOCUSED, activeLayerDrawable);

        return states;
    }

    private static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = 24;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    public static Drawable getDrawableFromFile(Context context, String fileName) {
        File file = new File(fileName);
        if (!file.exists()) {
            return null;
        }

        return new BitmapDrawable(context.getResources(),
                LockscreenTargetUtils.getRoundedCornerBitmap(BitmapFactory.decodeFile(fileName)));
    }

    public static Drawable getDrawableFromResources(Context context,
            String packageName, String identifier, boolean activated) {
        Resources res;

        if (packageName != null) {
            try {
                Context packageContext = context.createPackageContext(packageName, 0);
                res = context.getResources();
            } catch (PackageManager.NameNotFoundException e) {
                Log.w(TAG, "Could not fetch icons from package " + packageName);
                return null;
            }
        } else {
            res = context.getResources();
            packageName = "android";
        }

        if (activated) {
            identifier = identifier.replaceAll("_normal", "_activated");
        }

        try {
            int id = res.getIdentifier(identifier, "drawable", packageName);
            return res.getDrawable(id);
        } catch (Resources.NotFoundException e) {
            Log.w(TAG, "Could not resolve icon " + identifier + " in " + packageName, e);
        }

        return null;
    }
}
+109 −170
Original line number Diff line number Diff line
@@ -27,27 +27,13 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.InsetDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Slog;
@@ -55,6 +41,7 @@ import android.view.View;
import android.widget.LinearLayout;

import com.android.internal.telephony.IccCardConstants.State;
import com.android.internal.util.cm.LockscreenTargetUtils;
import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.multiwaveview.GlowPadView;
import com.android.internal.widget.multiwaveview.GlowPadView.OnTriggerListener;
@@ -80,7 +67,6 @@ public class KeyguardSelectorView extends LinearLayout implements KeyguardSecuri
    private String[] mStoredTargets;
    private int mTargetOffset;
    private boolean mIsScreenLarge;
    private int mCreationOrientation;

    OnTriggerListener mOnTriggerListener = new OnTriggerListener() {

@@ -112,21 +98,21 @@ public class KeyguardSelectorView extends LinearLayout implements KeyguardSecuri
                    break;
                }
            } else {
                final boolean isLand = mCreationOrientation == Configuration.ORIENTATION_LANDSCAPE;
                if ((target == 0 && (mIsScreenLarge || !isLand)) || (target == 2 && !mIsScreenLarge && isLand)) {
                if (target == mTargetOffset) {
                    mCallback.dismiss(false);
                } else {
                    target -= 1 + mTargetOffset;
                    if (target < mStoredTargets.length && mStoredTargets[target] != null) {
                        if (mStoredTargets[target].equals(GlowPadView.EMPTY_TARGET)) {
                    int realTarget = target - mTargetOffset - 1;
                    String targetUri = realTarget < mStoredTargets.length
                            ? mStoredTargets[realTarget] : null;

                    if (GlowPadView.EMPTY_TARGET.equals(targetUri)) {
                        mCallback.dismiss(false);
                    } else {
                        try {
                                Intent launchIntent = Intent.parseUri(mStoredTargets[target], 0);
                                mActivityLauncher.launchActivity(launchIntent, false, true, null, null);
                                return;
                            Intent intent = Intent.parseUri(targetUri, 0);
                            mActivityLauncher.launchActivity(intent, false, true, null, null);
                        } catch (URISyntaxException e) {
                            }
                            Log.w(TAG, "Invalid lockscreen target " + targetUri);
                        }
                    }
                }
@@ -187,16 +173,17 @@ public class KeyguardSelectorView extends LinearLayout implements KeyguardSecuri
        @Override
        Context getContext() {
            return mContext;
        }};
        }
    };

    public KeyguardSelectorView(Context context) {
        this(context, null);
        mCreationOrientation = Resources.getSystem().getConfiguration().orientation;
    }

    public KeyguardSelectorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mLockPatternUtils = new LockPatternUtils(getContext());
        mTargetOffset = LockscreenTargetUtils.getTargetOffset(context);
    }

    @Override
@@ -215,35 +202,6 @@ public class KeyguardSelectorView extends LinearLayout implements KeyguardSecuri
        mFadeView = carrierArea;
    }

    public boolean isScreenLarge() {
        final int screenSize = Resources.getSystem().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK;
        boolean isScreenLarge = screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE ||
                screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
        return isScreenLarge;
    }

    private StateListDrawable getLayeredDrawable(Drawable back, Drawable front, int inset, boolean frontBlank) {
        Resources res = getResources();
        InsetDrawable[] inactivelayer = new InsetDrawable[2];
        InsetDrawable[] activelayer = new InsetDrawable[2];
        inactivelayer[0] = new InsetDrawable(res.getDrawable(com.android.internal.R.drawable.ic_lockscreen_lock_pressed), 0, 0, 0, 0);
        inactivelayer[1] = new InsetDrawable(front, inset, inset, inset, inset);
        activelayer[0] = new InsetDrawable(back, 0, 0, 0, 0);
        activelayer[1] = new InsetDrawable(frontBlank ? res.getDrawable(android.R.color.transparent) : front, inset, inset, inset, inset);
        StateListDrawable states = new StateListDrawable();
        LayerDrawable inactiveLayerDrawable = new LayerDrawable(inactivelayer);
        inactiveLayerDrawable.setId(0, 0);
        inactiveLayerDrawable.setId(1, 1);
        LayerDrawable activeLayerDrawable = new LayerDrawable(activelayer);
        activeLayerDrawable.setId(0, 0);
        activeLayerDrawable.setId(1, 1);
        states.addState(TargetDrawable.STATE_INACTIVE, inactiveLayerDrawable);
        states.addState(TargetDrawable.STATE_ACTIVE, activeLayerDrawable);
        states.addState(TargetDrawable.STATE_FOCUSED, activeLayerDrawable);
        return states;
    }

    public boolean isTargetPresent(int resId) {
        return mGlowPadView.getTargetPosition(resId) != -1;
    }
@@ -263,7 +221,8 @@ public class KeyguardSelectorView extends LinearLayout implements KeyguardSecuri
                || secureCameraDisabled;
        final KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(getContext());
        boolean disabledBySimState = monitor.isSimLocked();
        boolean cameraPresent = mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
        boolean cameraPresent =
                mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
        boolean searchTargetPresent =
                isTargetPresent(com.android.internal.R.drawable.ic_action_assist_generic);

@@ -288,9 +247,9 @@ public class KeyguardSelectorView extends LinearLayout implements KeyguardSecuri
    }

    public void updateResources() {
        String storedVal = Settings.System.getStringForUser(mContext.getContentResolver(),
        String storedTargets = Settings.System.getStringForUser(mContext.getContentResolver(),
                Settings.System.LOCKSCREEN_TARGETS, UserHandle.USER_CURRENT);
        if (storedVal == null) {
        if (storedTargets == null) {
            // Update the search icon with drawable from the search .apk
            if (!mSearchDisabled) {
                Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
@@ -320,125 +279,105 @@ public class KeyguardSelectorView extends LinearLayout implements KeyguardSecuri
            // Enable magnetic targets
            mGlowPadView.setMagneticTargets(true);
        } else {
            mStoredTargets = storedVal.split("\\|");
            mIsScreenLarge = isScreenLarge();
            ArrayList<TargetDrawable> storedDraw = new ArrayList<TargetDrawable>();
            mStoredTargets = storedTargets.split("\\|");
            ArrayList<TargetDrawable> storedDrawables = new ArrayList<TargetDrawable>();

            final Resources res = getResources();
            final int targetInset = res.getDimensionPixelSize(com.android.internal.R.dimen.lockscreen_target_inset);
            final PackageManager packMan = mContext.getPackageManager();
            final boolean isLandscape = mCreationOrientation == Configuration.ORIENTATION_LANDSCAPE;
            final Drawable blankActiveDrawable = res.getDrawable(R.drawable.ic_lockscreen_target_activated);
            final int targetInset = res.getDimensionPixelSize(
                    com.android.internal.R.dimen.lockscreen_target_inset);
            final PackageManager pm = mContext.getPackageManager();

            final Drawable blankActiveDrawable = res.getDrawable(
                    R.drawable.ic_lockscreen_target_activated);
            final InsetDrawable activeBack = new InsetDrawable(blankActiveDrawable, 0, 0, 0, 0);

            // Disable magnetic target
            mGlowPadView.setMagneticTargets(false);

            // Magnetic target replacement
            final Drawable blankInActiveDrawable = res.getDrawable(com.android.internal.R.drawable.ic_lockscreen_lock_pressed);
            final Drawable unlockActiveDrawable = res.getDrawable(com.android.internal.R.drawable.ic_lockscreen_unlock_activated);
            final Drawable blankInActiveDrawable = res.getDrawable(
                    com.android.internal.R.drawable.ic_lockscreen_lock_pressed);
            final Drawable unlockActiveDrawable = res.getDrawable(
                    com.android.internal.R.drawable.ic_lockscreen_unlock_activated);

            // Shift targets for landscape lockscreen on phones
            mTargetOffset = isLandscape && !mIsScreenLarge ? 2 : 0;
            if (mTargetOffset == 2) {
                storedDraw.add(new TargetDrawable(res, null));
                storedDraw.add(new TargetDrawable(res, null));
            for (int i = 0; i < mTargetOffset; i++) {
                storedDrawables.add(new TargetDrawable(res, null));
            }

            // Add unlock target
            storedDraw.add(new TargetDrawable(res, res.getDrawable(R.drawable.ic_lockscreen_unlock)));
            storedDrawables.add(new TargetDrawable(res,
                    res.getDrawable(R.drawable.ic_lockscreen_unlock)));

            for (int i = 0; i < 8 - mTargetOffset - 1; i++) {
                int tmpInset = targetInset;
                if (i < mStoredTargets.length) {
                int inset = targetInset;
                if (i >= mStoredTargets.length) {
                    storedDrawables.add(new TargetDrawable(res, 0));
                    continue;
                }

                String uri = mStoredTargets[i];
                    if (!uri.equals(GlowPadView.EMPTY_TARGET)) {
                if (uri.equals(GlowPadView.EMPTY_TARGET)) {
                    Drawable d = LockscreenTargetUtils.getLayeredDrawable(
                            mContext, unlockActiveDrawable, blankInActiveDrawable, inset, true);
                    storedDrawables.add(new TargetDrawable(res, d));
                    continue;
                }

                try {
                            Intent in = Intent.parseUri(uri,0);
                    Intent intent = Intent.parseUri(uri, 0);
                    Drawable front = null;
                    Drawable back = activeBack;
                    boolean frontBlank = false;
                            if (in.hasExtra(GlowPadView.ICON_FILE)) {
                                String fSource = in.getStringExtra(GlowPadView.ICON_FILE);
                                if (fSource != null) {
                                    File fPath = new File(fSource);
                                    if (fPath.exists()) {
                                        front = new BitmapDrawable(res, getRoundedCornerBitmap(BitmapFactory.decodeFile(fSource)));
                                        tmpInset = tmpInset + 5;
                                    }
                                }
                            } else if (in.hasExtra(GlowPadView.ICON_RESOURCE)) {
                                String rSource = in.getStringExtra(GlowPadView.ICON_RESOURCE);
                                String rPackage = in.getStringExtra(GlowPadView.ICON_PACKAGE);
                                if (rSource != null) {
                                    if (rPackage != null) {
                                        try {
                                            Context rContext = mContext.createPackageContext(rPackage, 0);
                                            int id = rContext.getResources().getIdentifier(rSource, "drawable", rPackage);
                                            front = rContext.getResources().getDrawable(id);
                                            id = rContext.getResources().getIdentifier(rSource.replaceAll("_normal", "_activated"),
                                                    "drawable", rPackage);
                                            back = rContext.getResources().getDrawable(id);
                                            tmpInset = 0;
                                            frontBlank = true;
                                        } catch (NameNotFoundException e) {
                                            e.printStackTrace();
                                        } catch (NotFoundException e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        front = res.getDrawable(res.getIdentifier(rSource, "drawable", "android"));
                                        back = res.getDrawable(res.getIdentifier(
                                                rSource.replaceAll("_normal", "_activated"), "drawable", "android"));
                                        tmpInset = 0;

                    if (intent.hasExtra(GlowPadView.ICON_FILE)) {
                        front = LockscreenTargetUtils.getDrawableFromFile(mContext,
                                intent.getStringExtra(GlowPadView.ICON_FILE));
                        inset += 5;
                    } else if (intent.hasExtra(GlowPadView.ICON_RESOURCE)) {
                        String source = intent.getStringExtra(GlowPadView.ICON_RESOURCE);
                        String packageName = intent.getStringExtra(GlowPadView.ICON_PACKAGE);

                        if (source != null) {
                            front = LockscreenTargetUtils.getDrawableFromResources(mContext,
                                    packageName, source, false);
                            back = LockscreenTargetUtils.getDrawableFromResources(mContext,
                                    packageName, source, true);
                            inset = 0;
                            frontBlank = true;
                        }
                    }
                            }
                    if (front == null || back == null) {
                                ActivityInfo aInfo = in.resolveActivityInfo(packMan, PackageManager.GET_ACTIVITIES);
                                if (aInfo != null) {
                                    front = aInfo.loadIcon(packMan);
                        ActivityInfo activityInfo = intent.resolveActivityInfo(pm,
                                PackageManager.GET_ACTIVITIES);
                        if (activityInfo != null) {
                            front = activityInfo.loadIcon(pm);
                        } else {
                            front = res.getDrawable(android.R.drawable.sym_def_app_icon);
                        }
                    }
                            TargetDrawable nDrawable = new TargetDrawable(res, getLayeredDrawable(back,front, tmpInset, frontBlank));
                            ComponentName compName = in.getComponent();
                            if (compName != null) {
                                String cls = compName.getClassName();
                                if (cls.equals("com.android.camera.CameraLauncher")) {
                                    nDrawable.setEnabled(!mCameraDisabled);
                                } else if (cls.equals("SearchActivity")) {
                                    nDrawable.setEnabled(!mSearchDisabled);
                                }
                            }
                            storedDraw.add(nDrawable);
                        } catch (Exception e) {
                            storedDraw.add(new TargetDrawable(res, 0));
                        }
                    } else {
                        storedDraw.add(new TargetDrawable(res, getLayeredDrawable(unlockActiveDrawable, blankInActiveDrawable, tmpInset, true)));
                    }
                } else {
                    storedDraw.add(new TargetDrawable(res, 0));
                }

                    Drawable drawable = LockscreenTargetUtils.getLayeredDrawable(mContext,
                            back,front, inset, frontBlank);
                    TargetDrawable targetDrawable = new TargetDrawable(res, drawable);

                    ComponentName compName = intent.getComponent();
                    String className = compName == null ? null : compName.getClassName();
                    if (TextUtils.equals(className, "com.android.camera.CameraLauncher")) {
                        targetDrawable.setEnabled(!mCameraDisabled);
                    } else if (TextUtils.equals(className, "SearchActivity")) {
                        targetDrawable.setEnabled(!mSearchDisabled);
                    }
            mGlowPadView.setTargetResources(storedDraw);

                    storedDrawables.add(targetDrawable);
                } catch (URISyntaxException e) {
                    Log.w(TAG, "Invalid target uri " + uri);
                    storedDrawables.add(new TargetDrawable(res, 0));
                }
            }

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = 24;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
            mGlowPadView.setTargetResources(storedDrawables);
        }
    }

    void doTransition(View view, float to) {