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

Commit 3a7c15ea authored by Danny Baumann's avatar Danny Baumann
Browse files

Cleanup of lock screen widget code:

- Don't duplicate stringToLongArray function, put it in Settings class
  instead
- Remove unused argument from vibrate() function
- Don't hardcode time format strings, use DateFormat class instead
- Also use DateFormat class to retrieve 12/24 hour selection value

Change-Id: Idda45d97df0e966a1a9e208f9730284ed0268088
parent 88672d32
Loading
Loading
Loading
Loading
+68 −0
Original line number Diff line number Diff line
@@ -932,6 +932,74 @@ public final class Settings {
            return putString(cr, name, Long.toString(value));
        }

        /**
         * Convenience function for retrieving a single system settings value
         * as an array of {@code long}.  Note that internally setting values are always
         * stored as strings; this function converts the string to a {@code long}
         * array for you.  The default value will be returned if the setting is
         * not defined or not a {@code long} array.
         *
         * @param cr The ContentResolver to access.
         * @param name The name of the setting to retrieve.
         * @param def Value to return if the setting is not defined.
         *
         * @return The setting's current value, or 'def' if it is not defined
         * or not a valid {@code long}.
         */
        public static long[] getLongArray(ContentResolver cr, String name, long[] def) {
            String valString = getString(cr, name);
            long[] value;

            try {
                value = valString != null ? stringToLongArray(valString) : def;
            } catch (NumberFormatException e) {
                value = def;
            }
            return value;
        }

        /**
         * Convenience function for retrieving a single system settings value
         * as an array of {@code long}.  Note that internally setting values are always
         * stored as strings; this function converts the string to a {@code long} array
         * for you.
         * <p>
         * This version does not take a default value.  If the setting has not
         * been set, or the string value is not a number,
         * it throws {@link SettingNotFoundException}.
         *
         * @param cr The ContentResolver to access.
         * @param name The name of the setting to retrieve.
         *
         * @return The setting's current value.
         * @throws SettingNotFoundException Thrown if a setting by the given
         * name can't be found or the setting value is not a long array.
         */
        public static long[] getLongArray(ContentResolver cr, String name)
                throws SettingNotFoundException {
            String valString = getString(cr, name);
            try {
                return stringToLongArray(valString);
            } catch (NumberFormatException e) {
                throw new SettingNotFoundException(name);
            }
        }

        private static long[] stringToLongArray(String inpString)
                throws NumberFormatException {
            if (inpString == null) {
                throw new NumberFormatException();
            }
            String[] splitStr = inpString.split(",");
            int los = splitStr.length;
            long[] returnLong = new long[los];
            int i;
            for (i = 0; i < los; i++) {
                returnLong[i] = Long.parseLong(splitStr[i].trim());
            }
            return returnLong;
        }

        /**
         * Convenience function for retrieving a single system settings value
         * as a floating point number.  Note that internally setting values are
+24 −51
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.internal.widget;

import java.util.Date;
import android.provider.Settings;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
@@ -103,9 +104,6 @@ public class RotarySelector extends View {
    private boolean mLenseMode=false;
    // are we in rotary revamped mode?
    private boolean mRevampedMode=false;
    // time format from system settings - contains 12 or 24
    private int mTime12_24 = 12;


    // state of the animation used to bring the handle back to its start position when
    // the user lets go before triggering an action
@@ -152,8 +150,6 @@ public class RotarySelector extends View {

    // Vibration (haptic feedback)
    private Vibrator mVibrator;
    private static final long VIBRATE_SHORT = 30;  // msec
    private static final long VIBRATE_LONG = 40;  // msec

    /**
     * The drawable for the arrows need to be scrunched this many dips towards the rotary bg below
@@ -306,6 +302,7 @@ public class RotarySelector extends View {
        }

        mDateFormatString = context.getString(R.string.full_wday_month_day_no_year);
        mVibrator = (android.os.Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
    }

    private Bitmap getBitmapFor(int resId) {
@@ -446,24 +443,24 @@ public class RotarySelector extends View {
        // for lense mode, we are done after time and date
        if(mLenseMode){
            if(isHoriz()){
                Time mTime = new Time();
                mTime.setToNow();

                String mTimeString;
                if(mTime12_24==24)
                    mTimeString=mTime.format("%R");
                else
                    mTimeString=mTime.format("%l:%M %P");
                String mDate=(String) DateFormat.format(mDateFormatString, new Date());
                Date now = new Date();
                String timeString = DateFormat.getTimeFormat(mContext).format(now).toString();
                String dateString = DateFormat.format(mDateFormatString, now).toString();

                //Add offset specified in config file
                int customTimeOffset = getContext().getResources().getInteger(R.integer.config_lenseTimeLabelOffsetDIP);

                canvas.translate(0, 0);
                mLensePaint.setTextSize(LENSE_TIME_SIZE_DIP * mDensity * mDensityScaleFactor);
                canvas.drawText(mTimeString, mBackgroundWidth / 2 * mDensityScaleFactor, mRotaryOffsetY + mMarginBottom + LENSE_TIME_SIZE_DIP * mDensity + customTimeOffset, mLensePaint);
                canvas.drawText(timeString,
                                mBackgroundWidth / 2 * mDensityScaleFactor,
                                mRotaryOffsetY + mMarginBottom + LENSE_TIME_SIZE_DIP * mDensity + customTimeOffset,
                                mLensePaint);
                mLensePaint.setTextSize(LENSE_DATE_SIZE_DIP * mDensity * mDensityScaleFactor);
                canvas.drawText(mDate, mBackgroundWidth / 2 * mDensityScaleFactor, mRotaryOffsetY + mMarginBottom + LENSE_DATE_SIZE_DIP * mDensity * 3 + customTimeOffset, mLensePaint);
                canvas.drawText(dateString,
                                mBackgroundWidth / 2 * mDensityScaleFactor,
                                mRotaryOffsetY + mMarginBottom + LENSE_DATE_SIZE_DIP * mDensity * 3 + customTimeOffset,
                                mLensePaint);
            }
            return;
        }
@@ -720,23 +717,23 @@ public class RotarySelector extends View {
                if (mLenseMode){
                    setGrabbedState(MID_HANDLE_GRABBED);
                    invalidate();
                    vibrate(VIBRATE_SHORT);
                    vibrate();
                    break;
                }
                if (eventX < mLeftHandleX + hitWindow) {
                    mRotaryOffsetX = eventX - mLeftHandleX;
                    setGrabbedState(LEFT_HANDLE_GRABBED);
                    invalidate();
                    vibrate(VIBRATE_SHORT);
                    vibrate();
                } else if (eventX > mMidHandleX - hitWindow && eventX <= mRightHandleX - hitWindow && mCustomAppDimple) {
                    setGrabbedState(MID_HANDLE_GRABBED);
                    invalidate();
                    vibrate(VIBRATE_SHORT);
                    vibrate();
                } else if (eventX > mRightHandleX - hitWindow) {
                    mRotaryOffsetX = eventX - mRightHandleX;
                    setGrabbedState(RIGHT_HANDLE_GRABBED);
                    invalidate();
                    vibrate(VIBRATE_SHORT);
                    vibrate();
                }

                break;
@@ -926,14 +923,13 @@ public class RotarySelector extends View {
    /**
     * Triggers haptic feedback.
     */
    private synchronized void vibrate(long duration) {
        if (mVibrator == null) {
            mVibrator = (android.os.Vibrator)
                    getContext().getSystemService(Context.VIBRATOR_SERVICE);
    private synchronized void vibrate() {
        ContentResolver cr = mContext.getContentResolver();
        final boolean hapticsEnabled = Settings.System.getInt(cr, Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 1;
        if (hapticsEnabled) {
            long[] hapFeedback = Settings.System.getLongArray(cr, Settings.System.HAPTIC_DOWN_ARRAY, new long[] { 0 });
            mVibrator.vibrate(hapFeedback, -1);
        }
        final boolean hapticsEnabled = Settings.System.getInt(mContext.getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 1;
        long[] hapFeedback = stringToLongArray(Settings.System.getString(getContext().getContentResolver(),Settings.System.HAPTIC_DOWN_ARRAY));
        if (hapticsEnabled) mVibrator.vibrate(hapFeedback, -1);
    }

    /**
@@ -963,7 +959,7 @@ public class RotarySelector extends View {
     * Dispatches a trigger event to our listener.
     */
    private void dispatchTriggerEvent(int whichHandle) {
        vibrate(VIBRATE_LONG);
        vibrate();
        if (mOnDialTriggerListener != null) {
            mOnDialTriggerListener.onDialTrigger(this, whichHandle);
        }
@@ -1076,29 +1072,6 @@ public class RotarySelector extends View {
        }
    }

    /**
     *  Sets the time format for propper display in lense style - called from LockScreen.java
     */
    public void setTimeFormat(int time12_24){
        mTime12_24=time12_24;
    }

    private long[] stringToLongArray(String inpString) {
        if (inpString == null) {
            long[] returnLong = new long[1];
            returnLong[0] = 0;
            return returnLong;
        }
        String[] splitStr = inpString.split(",");
        int los = splitStr.length;
        long[] returnLong = new long[los];
        int i;
        for (i = 0; i < los; i++) {
            returnLong[i] = Long.parseLong(splitStr[i].trim());
        }
        return returnLong;
    }

    // Debugging / testing code

    private void log(String msg) {
+11 −28
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.internal.widget;
import android.provider.Settings;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
@@ -57,8 +58,6 @@ public class SlidingTab extends ViewGroup {

    // TODO: Make these configurable
    private static final float THRESHOLD = 2.0f / 3.0f;
    private static final long VIBRATE_SHORT = 30;
    private static final long VIBRATE_LONG = 40;
    private static final int TRACKING_MARGIN = 50;
    private static final int ANIM_DURATION = 250; // Time for most animations (in ms)
    private static final int ANIM_TARGET_TIME = 500; // Time to show targets (in ms)
@@ -464,6 +463,7 @@ public class SlidingTab extends ViewGroup {
                R.drawable.jog_tab_bar_right_generic,
                R.drawable.jog_tab_target_gray);

        mVibrator = (android.os.Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
        // setBackgroundColor(0x80808080);
    }

@@ -525,7 +525,7 @@ public class SlidingTab extends ViewGroup {
            case MotionEvent.ACTION_DOWN: {
                mTracking = true;
                mTriggered = false;
                vibrate(VIBRATE_SHORT);
                vibrate();
                if (leftHit) {
                    mCurrentSlider = mLeftSlider;
                    mOtherSlider = mRightSlider;
@@ -804,14 +804,13 @@ public class SlidingTab extends ViewGroup {
    /**
     * Triggers haptic feedback.
     */
    private synchronized void vibrate(long duration) {
        if (mVibrator == null) {
            mVibrator = (android.os.Vibrator)
                    getContext().getSystemService(Context.VIBRATOR_SERVICE);
    private synchronized void vibrate() {
        ContentResolver cr = mContext.getContentResolver();
        final boolean hapticsEnabled = Settings.System.getInt(cr, Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 1;
        if (hapticsEnabled) {
            long[] hapFeedback = Settings.System.getLongArray(cr, Settings.System.HAPTIC_DOWN_ARRAY, new long[] { 0 });
            mVibrator.vibrate(hapFeedback, -1);
        }
        final boolean hapticsEnabled = Settings.System.getInt(mContext.getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 1;
        long[] hapFeedback = stringToLongArray(Settings.System.getString(getContext().getContentResolver(),Settings.System.HAPTIC_DOWN_ARRAY));
        if (hapticsEnabled) mVibrator.vibrate(hapFeedback, -1);
    }

    /**
@@ -828,7 +827,7 @@ public class SlidingTab extends ViewGroup {
     * @param whichHandle the handle that triggered the event.
     */
    private void dispatchTriggerEvent(int whichHandle) {
        vibrate(VIBRATE_LONG);
        vibrate();
        if (mOnTriggerListener != null) {
            mOnTriggerListener.onTrigger(this, whichHandle);
        }
@@ -847,22 +846,6 @@ public class SlidingTab extends ViewGroup {
        }
    }

     private long[] stringToLongArray(String inpString) {
        if (inpString == null) {
            long[] returnLong = new long[1];
            returnLong[0] = 0;
            return returnLong;
        }
        String[] splitStr = inpString.split(",");
        int los = splitStr.length;
        long[] returnLong = new long[los];
        int i;
        for (i = 0; i < los; i++) {
            returnLong[i] = Long.parseLong(splitStr[i].trim());
        }
        return returnLong;
    }

    private void log(String msg) {
        Log.d(LOG_TAG, msg);
    }
+1 −6
Original line number Diff line number Diff line
@@ -131,10 +131,6 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM
    private String mDateFormatString;
    private boolean mEnableMenuKeyInLockScreen;

    // time format from system settings - contains 12 or 24
    private int mTime12_24 = (Settings.System.getInt(mContext.getContentResolver(),
            Settings.System.TIME_12_24, 12));

    private boolean mTrackballUnlockScreen = (Settings.System.getInt(mContext.getContentResolver(),
            Settings.System.TRACKBALL_UNLOCK_SCREEN, 0) == 1);

@@ -427,7 +423,6 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM
        mRotarySelector.setLenseSquare(mUseRotaryRevLockscreen);
        if(mRotaryHideArrows)
            mRotarySelector.hideArrows(true);
        mRotarySelector.setTimeFormat(mTime12_24);

        //hide most items when we are in potrait lense mode
        mLensePortrait=(mUseLenseSquareLockscreen && mCreationOrientation != Configuration.ORIENTATION_LANDSCAPE);
@@ -1313,7 +1308,7 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM
        mNowPlaying.setVisibility(visibility);
        mAlbumArt.setVisibility(visibility);

        if (mTime12_24 == 24)
        if (DateFormat.is24HourFormat(mContext))
            mAmPm.setVisibility(View.INVISIBLE);

        mNowPlayingToggle = false;