Example usage for android.app WallpaperManager peekFastDrawable

List of usage examples for android.app WallpaperManager peekFastDrawable

Introduction

In this page you can find the example usage for android.app WallpaperManager peekFastDrawable.

Prototype

@RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
public Drawable peekFastDrawable() 

Source Link

Document

Like #getFastDrawable() , but if there is no wallpaper set, a null pointer is returned.

Usage

From source file:com.moonpi.tapunlock.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get lobster_two asset and create typeface
    // Set action bar title to lobster_two typeface
    lobsterTwo = Typeface.createFromAsset(getAssets(), "lobster_two.otf");

    int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    actionBarTitleView = (TextView) getWindow().findViewById(actionBarTitle);

    if (actionBarTitleView != null) {
        actionBarTitleView.setTypeface(lobsterTwo);
        actionBarTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 28f);
        actionBarTitleView.setTextColor(getResources().getColor(R.color.blue));
    }/*from  w  w w. j  a  v a2  s.c o  m*/

    setContentView(R.layout.activity_main);

    // Hide keyboard on app launch
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    // Get NFC service and adapter
    NfcManager nfcManager = (NfcManager) this.getSystemService(Context.NFC_SERVICE);
    nfcAdapter = nfcManager.getDefaultAdapter();

    // Create PendingIntent for enableForegroundDispatch for NFC tag discovery
    pIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    readFromJSON();
    writeToJSON();
    readFromJSON();

    // If Android 4.2 or bigger
    if (Build.VERSION.SDK_INT > 16) {
        // Check if TapUnlock folder exists, if not, create directory
        File folder = new File(Environment.getExternalStorageDirectory() + "/TapUnlock");
        boolean folderSuccess = true;

        if (!folder.exists()) {
            folderSuccess = folder.mkdir();
        }

        try {
            // If blur var bigger than 0
            if (settings.getInt("blur") > 0) {
                // If folder exists or successfully created
                if (folderSuccess) {
                    // If blurred wallpaper file doesn't exist
                    if (!ImageUtils.doesBlurredWallpaperExist()) {
                        // Get default wallpaper
                        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
                        final Drawable wallpaperDrawable = wallpaperManager.peekFastDrawable();

                        if (wallpaperDrawable != null) {
                            // Default wallpaper to bitmap - fastBlur the bitmap - store bitmap
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    Bitmap bitmapToBlur = ImageUtils.drawableToBitmap(wallpaperDrawable);

                                    Bitmap blurredWallpaper = null;
                                    if (bitmapToBlur != null)
                                        blurredWallpaper = ImageUtils.fastBlur(MainActivity.this, bitmapToBlur,
                                                blur);

                                    if (blurredWallpaper != null) {
                                        ImageUtils.storeImage(blurredWallpaper);
                                    }
                                }
                            }).start();
                        }
                    }
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    // Initialize layout items
    pinEdit = (EditText) findViewById(R.id.pinEdit);
    pinEdit.setImeOptions(EditorInfo.IME_ACTION_DONE);
    Button setPin = (Button) findViewById(R.id.setPin);
    ImageButton newTag = (ImageButton) findViewById(R.id.newTag);
    enabled_disabled = (TextView) findViewById(R.id.enabled_disabled);
    Switch toggle = (Switch) findViewById(R.id.toggle);
    seekBar = (SeekBar) findViewById(R.id.seekBar);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    Button refreshWallpaper = (Button) findViewById(R.id.refreshWallpaper);
    listView = (ListView) findViewById(R.id.listView);
    backgroundBlurValue = (TextView) findViewById(R.id.backgroundBlurValue);
    noTags = (TextView) findViewById(R.id.noTags);

    // Initialize TagAdapter
    adapter = new TagAdapter(this, tags);

    registerForContextMenu(listView);

    // Set listView adapter to TapAdapter object
    listView.setAdapter(adapter);

    // Set click, check and seekBar listeners
    setPin.setOnClickListener(this);
    newTag.setOnClickListener(this);
    refreshWallpaper.setOnClickListener(this);
    toggle.setOnCheckedChangeListener(this);
    seekBar.setOnSeekBarChangeListener(this);

    // Set seekBar progress to blur var
    try {
        seekBar.setProgress(settings.getInt("blur"));

    } catch (JSONException e) {
        e.printStackTrace();
    }

    // Refresh the listView height
    updateListViewHeight(listView);

    // If no tags, show 'Press + to add Tags' textView
    if (tags.length() == 0)
        noTags.setVisibility(View.VISIBLE);

    else
        noTags.setVisibility(View.INVISIBLE);

    // Scroll up
    scrollView = (ScrollView) findViewById(R.id.scrollView);

    scrollView.post(new Runnable() {
        public void run() {
            scrollView.scrollTo(0, scrollView.getTop());
            scrollView.fullScroll(View.FOCUS_UP);
        }
    });

    // If lockscreen enabled, initialize switch, text and start service
    try {
        if (settings.getBoolean("lockscreen")) {
            onStart = true;
            enabled_disabled.setText(R.string.lockscreen_enabled);
            enabled_disabled.setTextColor(getResources().getColor(R.color.green));

            toggle.setChecked(true);
        }

    } catch (JSONException e1) {
        e1.printStackTrace();
    }
}

From source file:com.moonpi.tapunlock.MainActivity.java

@Override
public void onClick(View v) {
    // If OK(setPin) clicked, ask user if sure; if yes, store PIN; else, go back
    if (v.getId() == R.id.setPin) {
        // If PIN length between 4 and 6, store PIN and toast successful
        if (pinEdit.length() >= 4 && pinEdit.length() <= 6) {
            new AlertDialog.Builder(this).setMessage(R.string.set_pin_confirmation)
                    .setPositiveButton(R.string.yes_button, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                settings.put("pin", String.valueOf(pinEdit.getText()));

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }//  ww w . ja v  a 2s . c  om

                            writeToJSON();

                            Toast toast = Toast.makeText(getApplicationContext(), R.string.toast_pin_set,
                                    Toast.LENGTH_SHORT);
                            toast.show();

                            imm.hideSoftInputFromWindow(pinEdit.getWindowToken(), 0);

                        }
                    }).setNegativeButton(R.string.no_button, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            imm.hideSoftInputFromWindow(pinEdit.getWindowToken(), 0);
                            // Do nothing, close dialog
                        }
                    }).show();
        }

        // Toast user that PIN needs to be at least 4 digits long
        else {
            Toast toast = Toast.makeText(getApplicationContext(), R.string.toast_pin_needs4digits,
                    Toast.LENGTH_LONG);
            toast.show();
        }
    }

    // If 'Refresh wallpaper' pressed, check if Android 4.2 or above, if yes
    // Store new blur var, if blur bigger than 0 re-blur wallpaper
    else if (v.getId() == R.id.refreshWallpaper) {
        if (Build.VERSION.SDK_INT > 16) {
            try {
                settings.put("blur", blur);

            } catch (JSONException e) {
                e.printStackTrace();
            }

            writeToJSON();

            // If blur is 0, don't change anything, just toast
            if (blur == 0) {
                Toast toast = Toast.makeText(getApplicationContext(), R.string.toast_wallpaper_refreshed,
                        Toast.LENGTH_SHORT);
                toast.show();
            }

            // If blur is bigger than 0, get default wallpaper - to bitmap - fastblur bitmap - store
            else {
                // Check if TapUnlock folder exists, if not, create directory
                File folder = new File(Environment.getExternalStorageDirectory() + "/TapUnlock");
                boolean folderSuccess = true;

                if (!folder.exists()) {
                    folderSuccess = folder.mkdir();
                }

                if (folderSuccess) {
                    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
                    final Drawable wallpaperDrawable = wallpaperManager.peekFastDrawable();

                    if (wallpaperDrawable != null) {
                        // Display indeterminate progress bar while blurring
                        progressBar.setVisibility(View.VISIBLE);

                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Bitmap bitmapToBlur = ImageUtils.drawableToBitmap(wallpaperDrawable);

                                Bitmap blurredWallpaper = null;
                                if (bitmapToBlur != null)
                                    blurredWallpaper = ImageUtils.fastBlur(MainActivity.this, bitmapToBlur,
                                            blur);

                                boolean stored = false;
                                if (blurredWallpaper != null) {
                                    stored = ImageUtils.storeImage(blurredWallpaper);

                                    final boolean finalStored = stored;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            progressBar.setVisibility(View.INVISIBLE);

                                            if (finalStored) {
                                                Toast toast = Toast.makeText(getApplicationContext(),
                                                        R.string.toast_wallpaper_refreshed, Toast.LENGTH_SHORT);
                                                toast.show();
                                            }
                                        }
                                    });
                                }

                                if (bitmapToBlur == null || blurredWallpaper == null || !stored) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            progressBar.setVisibility(View.INVISIBLE);

                                            Toast toast = Toast.makeText(getApplicationContext(),
                                                    R.string.toast_wallpaper_not_refreshed, Toast.LENGTH_SHORT);
                                            toast.show();
                                        }
                                    });
                                }
                            }
                        }).start();
                    }

                    else {
                        Toast toast = Toast.makeText(getApplicationContext(),
                                R.string.toast_wallpaper_not_refreshed, Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
            }
        }

        // If Android version less than 4.2, display toast cannot blur
        else {
            Toast toast = Toast.makeText(getApplicationContext(), R.string.toast_cannot_blur,
                    Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    // If '+' pressed
    else if (v.getId() == R.id.newTag) {
        if (nfcAdapter != null) {
            // If NFC is on, show scan dialog and enableForegroundDispatch
            if (nfcAdapter.isEnabled()) {
                nfcAdapter.enableForegroundDispatch(this, pIntent,
                        new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) },
                        new String[][] { new String[] { "android.nfc.tech.MifareClassic" },
                                new String[] { "android.nfc.tech.MifareUltralight" },
                                new String[] { "android.nfc.tech.NfcA" },
                                new String[] { "android.nfc.tech.NfcB" },
                                new String[] { "android.nfc.tech.NfcF" },
                                new String[] { "android.nfc.tech.NfcV" },
                                new String[] { "android.nfc.tech.Ndef" },
                                new String[] { "android.nfc.tech.IsoDep" },
                                new String[] { "android.nfc.tech.NdefFormatable" } });

                MainActivity.this.showDialog(DIALOG_READ);
            }

            // NFC is off, prompt user to enable it and send him to NFC settings
            else {
                new AlertDialog.Builder(this).setTitle(R.string.nfc_off_dialog_title)
                        .setMessage(R.string.nfc_off_dialog_message)
                        .setPositiveButton(R.string.yes_button, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Settings.ACTION_NFC_SETTINGS);
                                startActivity(intent);
                            }
                        }).setNegativeButton(R.string.no_button, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // Do nothing, close dialog
                            }
                        }).show();
            }
        }

        // NFC adapter is null
        else {
            new AlertDialog.Builder(this).setTitle(R.string.nfc_off_dialog_title)
                    .setMessage(R.string.nfc_off_dialog_message)
                    .setPositiveButton(R.string.yes_button, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(Settings.ACTION_NFC_SETTINGS);
                            startActivity(intent);
                        }
                    }).setNegativeButton(R.string.no_button, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing, close dialog
                        }
                    }).show();
        }
    }
}