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

Commit fedc2db4 authored by Danesh M's avatar Danesh M Committed by Steve Kondik
Browse files

Settings : Lockscreen background customization (2/2)

Allow user to customize lockscreen background (port from cm7)

Patchset 2 : Remove default button from color dialog
Patchset 3 : Update strings according to request
Patchset 4 : String cleanup
             Switch to png
Patchset 5 : Summary cleanup
             ColorPicker value check
Patchset 6 : Shorten preference title
             Move preference up
Patchset 7 : Restore consumed newline

Change-Id: I104382548dcb64db52a06e24922687fbae1e32a8
parent fec3131a
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -1231,5 +1231,17 @@
        <item>2</item>
    </string-array>

    <string-array name="lockscreen_background_entries" translatable="false">
        <item>@string/lockscreen_background_color_fill</item>
        <item>@string/lockscreen_background_custom_image</item>
        <item>@string/lockscreen_background_default_wallpaper</item>
    </string-array>

    <string-array name="lockscreen_background_values" translatable="false">
        <item>0</item>
        <item>1</item>
        <item>2</item>
    </string-array>

<!-- **** CYANOGENMOD EDITS END **** -->
</resources>
+9 −0
Original line number Diff line number Diff line
@@ -1003,6 +1003,15 @@
    <string name="calendar_metadata_first_line">Show first line</string>
    <string name="calendar_metadata_all">Show all</string>

    <!-- Lock screen background -->
    <string name="lockscreen_custom_background_title">Background</string>
    <string name="lockscreen_custom_background_dialog_title">Choose color</string>
    <string name="lockscreen_background_result_successful">Background changed</string>
    <string name="lockscreen_background_result_not_successful">Background not changed</string>
    <string name="lockscreen_background_color_fill">Color fill</string>
    <string name="lockscreen_background_custom_image">Custom image</string>
    <string name="lockscreen_background_default_wallpaper">Default wallpaper</string>

    <!-- Wallpaper settings title [CHAR LIMIT=30] -->
    <string name="wallpaper_settings_title">Wallpaper</string>
    <!-- Wallpaper settings fragment title [CHAR LIMIT=30] -->
+8 −0
Original line number Diff line number Diff line
@@ -23,6 +23,14 @@
         android:title="@string/screen_security_category"
         android:summary="@string/screen_security_summary" />

     <ListPreference
        android:key="lockscreen_background"
        android:persistent="false"
        android:dialogTitle="@string/lockscreen_custom_background_title"
        android:title="@string/lockscreen_custom_background_title"
        android:entries="@array/lockscreen_background_entries"
        android:entryValues="@array/lockscreen_background_values"/>

     <PreferenceScreen
         android:fragment="com.android.settings.OwnerInfoSettings"
         android:key="owner_info_settings"
+138 −5
Original line number Diff line number Diff line
@@ -16,33 +16,79 @@

package com.android.settings.cyanogenmod;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.provider.MediaStore;
import android.provider.Settings;
import android.view.Window;
import android.widget.Toast;

import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.notificationlight.ColorPickerView;

public class LockscreenInterface extends SettingsPreferenceFragment implements
        Preference.OnPreferenceChangeListener {
    private static final String TAG = "LockscreenInterface";

    private static final int LOCKSCREEN_BACKGROUND = 1024;
    public static final String KEY_WEATHER_PREF = "lockscreen_weather";
    public static final String KEY_CALENDAR_PREF = "lockscreen_calendar";
    public static final String KEY_BACKGROUND_PREF = "lockscreen_background";
    private ListPreference mCustomBackground;
    private Preference mWeatherPref;
    private Preference mCalendarPref;
    private Activity mActivity;
    ContentResolver mResolver;

    private File wallpaperImage;
    private File wallpaperTemporary;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mResolver = getActivity().getContentResolver();
        mActivity = getActivity();
        mResolver = mActivity.getContentResolver();

        addPreferencesFromResource(R.xml.lockscreen_interface_settings);
        mWeatherPref = (Preference) findPreference(KEY_WEATHER_PREF);
        mCalendarPref = (Preference) findPreference(KEY_CALENDAR_PREF);
        mCustomBackground = (ListPreference) findPreference(KEY_BACKGROUND_PREF);
        mCustomBackground.setOnPreferenceChangeListener(this);
        wallpaperImage = new File(mActivity.getFilesDir()+"/lockwallpaper");
        wallpaperTemporary = new File(mActivity.getCacheDir()+"/lockwallpaper.tmp");
        updateCustomBackgroundSummary();
    }

    private void updateCustomBackgroundSummary() {
        int resId;
        String value = Settings.System.getString(getContentResolver(),
                Settings.System.LOCKSCREEN_BACKGROUND);
        if (value == null) {
            resId = R.string.lockscreen_background_default_wallpaper;
            mCustomBackground.setValueIndex(2);
        } else if (value.isEmpty()) {
            resId = R.string.lockscreen_background_custom_image;
            mCustomBackground.setValueIndex(1);
        } else {
            resId = R.string.lockscreen_background_color_fill;
            mCustomBackground.setValueIndex(0);
        }
        mCustomBackground.setSummary(getResources().getString(resId));
    }

    @Override
@@ -80,14 +126,101 @@ public class LockscreenInterface extends SettingsPreferenceFragment implements
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == LOCKSCREEN_BACKGROUND) {
            if (resultCode == Activity.RESULT_OK) {
                if (wallpaperTemporary.exists()) {
                    wallpaperTemporary.renameTo(wallpaperImage);
                }
                wallpaperImage.setReadOnly();
                Toast.makeText(mActivity, getResources().getString(R.string.
                        lockscreen_background_result_successful), Toast.LENGTH_LONG).show();
                Settings.System.putString(getContentResolver(),
                        Settings.System.LOCKSCREEN_BACKGROUND,"");
                updateCustomBackgroundSummary();
            } else {
                if (wallpaperTemporary.exists()) {
                    wallpaperTemporary.delete();
                }
                Toast.makeText(mActivity, getResources().getString(R.string.
                        lockscreen_background_result_not_successful), Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }

    public boolean onPreferenceChange(Preference preference, Object objValue) {
        final String key = preference.getKey();

        if (preference == mCustomBackground) {
            int indexOf = mCustomBackground.findIndexOfValue(objValue.toString());
            switch (indexOf) {
            //Displays color dialog when user has chosen color fill
            case 0:
                final ColorPickerView colorView = new ColorPickerView(mActivity);
                int currentColor = Settings.System.getInt(getContentResolver(),
                        Settings.System.LOCKSCREEN_BACKGROUND, -1);
                if (currentColor != -1) {
                    colorView.setColor(currentColor);
                }
                colorView.setAlphaSliderVisible(true);
                new AlertDialog.Builder(mActivity)
                .setTitle(R.string.lockscreen_custom_background_dialog_title)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Settings.System.putInt(getContentResolver(), Settings.System.LOCKSCREEN_BACKGROUND, colorView.getColor());
                        updateCustomBackgroundSummary();
                    }
                }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setView(colorView).show();
                return false;
            //Launches intent for user to select an image/crop it to set as background
            case 1:
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                intent.setType("image/*");
                intent.putExtra("crop", "true");
                intent.putExtra("scale", true);
                intent.putExtra("scaleUpIfNeeded", false);
                intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
                int width = mActivity.getWindowManager().getDefaultDisplay().getWidth();
                int height = mActivity.getWindowManager().getDefaultDisplay().getHeight();
                Rect rect = new Rect();
                Window window = mActivity.getWindow();
                window.getDecorView().getWindowVisibleDisplayFrame(rect);
                int statusBarHeight = rect.top;
                int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                int titleBarHeight = contentViewTop - statusBarHeight;
                boolean isPortrait = getResources().getConfiguration().orientation ==
                    Configuration.ORIENTATION_PORTRAIT;
                intent.putExtra("aspectX", isPortrait ? width : height - titleBarHeight);
                intent.putExtra("aspectY", isPortrait ? height - titleBarHeight : width);
                try {
                    wallpaperTemporary.createNewFile();
                    wallpaperTemporary.setWritable(true, false);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(wallpaperTemporary));
                    intent.putExtra("return-data", false);
                    mActivity.startActivityFromFragment(this, intent, LOCKSCREEN_BACKGROUND);
                } catch (IOException e) {
                } catch (ActivityNotFoundException e) {
                }
                return false;
            //Sets background color to default
            case 2:
                Settings.System.putString(getContentResolver(),
                        Settings.System.LOCKSCREEN_BACKGROUND, null);
                updateCustomBackgroundSummary();
                break;
            }
            return true;
        }
        return false;
    }
}