Search in sources :

Example 1 with OnApplyWindowInsetsListener

use of android.support.v4.view.OnApplyWindowInsetsListener in project muzei by romannurik.

the class GallerySettingsActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery_activity);
    Toolbar appBar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(appBar);
    getSupportLoaderManager().initLoader(0, null, this);
    bindService(new Intent(this, GalleryArtSource.class).setAction(GalleryArtSource.ACTION_BIND_GALLERY), mServiceConnection, BIND_AUTO_CREATE);
    mPlaceholderDrawable = new ColorDrawable(ContextCompat.getColor(this, R.color.gallery_chosen_photo_placeholder));
    mPlaceholderSmallDrawable = new ColorDrawable(ContextCompat.getColor(this, R.color.gallery_chosen_photo_placeholder));
    mPhotoGridView = (RecyclerView) findViewById(R.id.photo_grid);
    DefaultItemAnimator itemAnimator = new DefaultItemAnimator();
    itemAnimator.setSupportsChangeAnimations(false);
    mPhotoGridView.setItemAnimator(itemAnimator);
    setupMultiSelect();
    final GridLayoutManager gridLayoutManager = new GridLayoutManager(GallerySettingsActivity.this, 1);
    mPhotoGridView.setLayoutManager(gridLayoutManager);
    final ViewTreeObserver vto = mPhotoGridView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            int width = mPhotoGridView.getWidth() - mPhotoGridView.getPaddingStart() - mPhotoGridView.getPaddingEnd();
            if (width <= 0) {
                return;
            }
            // Compute number of columns
            int maxItemWidth = getResources().getDimensionPixelSize(R.dimen.gallery_chosen_photo_grid_max_item_size);
            int numColumns = 1;
            while (true) {
                if (width / numColumns > maxItemWidth) {
                    ++numColumns;
                } else {
                    break;
                }
            }
            int spacing = getResources().getDimensionPixelSize(R.dimen.gallery_chosen_photo_grid_spacing);
            mItemSize = (width - spacing * (numColumns - 1)) / numColumns;
            // Complete setup
            gridLayoutManager.setSpanCount(numColumns);
            mChosenPhotosAdapter.setHasStableIds(true);
            mPhotoGridView.setAdapter(mChosenPhotosAdapter);
            mPhotoGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            tryUpdateSelection(false);
        }
    });
    ViewCompat.setOnApplyWindowInsetsListener(mPhotoGridView, new OnApplyWindowInsetsListener() {

        @Override
        public WindowInsetsCompat onApplyWindowInsets(final View v, final WindowInsetsCompat insets) {
            int gridSpacing = getResources().getDimensionPixelSize(R.dimen.gallery_chosen_photo_grid_spacing);
            ViewCompat.onApplyWindowInsets(v, insets.replaceSystemWindowInsets(insets.getSystemWindowInsetLeft() + gridSpacing, gridSpacing, insets.getSystemWindowInsetRight() + gridSpacing, insets.getSystemWindowInsetBottom() + insets.getSystemWindowInsetTop() + gridSpacing + getResources().getDimensionPixelSize(R.dimen.gallery_fab_space)));
            return insets;
        }
    });
    Button enableRandomImages = (Button) findViewById(R.id.gallery_enable_random);
    enableRandomImages.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View view) {
            ActivityCompat.requestPermissions(GallerySettingsActivity.this, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, REQUEST_STORAGE_PERMISSION);
        }
    });
    Button permissionSettings = (Button) findViewById(R.id.gallery_edit_permission_settings);
    permissionSettings.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View view) {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", getPackageName(), null));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });
    mAddButton = findViewById(R.id.add_fab);
    mAddButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // On Lollipop and higher, we show the add toolbar to allow users to add either
                // individual photos or a whole directory
                showAddToolbar();
            } else {
                requestPhotos();
            }
        }
    });
    mAddToolbar = findViewById(R.id.add_toolbar);
    findViewById(R.id.add_photos).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View v) {
            requestPhotos();
        }
    });
    findViewById(R.id.add_folder).setOnClickListener(new View.OnClickListener() {

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onClick(final View v) {
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
            try {
                startActivityForResult(intent, REQUEST_CHOOSE_FOLDER);
                SharedPreferences preferences = getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
                if (preferences.getBoolean(SHOW_INTERNAL_STORAGE_MESSAGE, true)) {
                    Toast.makeText(GallerySettingsActivity.this, R.string.gallery_internal_storage_message, Toast.LENGTH_LONG).show();
                }
            } catch (ActivityNotFoundException e) {
                Snackbar.make(mPhotoGridView, R.string.gallery_add_folder_error, Snackbar.LENGTH_LONG).show();
                hideAddToolbar(true);
            }
        }
    });
}
Also used : SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) WindowInsetsCompat(android.support.v4.view.WindowInsetsCompat) OnApplyWindowInsetsListener(android.support.v4.view.OnApplyWindowInsetsListener) ColorDrawable(android.graphics.drawable.ColorDrawable) GridLayoutManager(android.support.v7.widget.GridLayoutManager) Button(android.widget.Button) ActivityNotFoundException(android.content.ActivityNotFoundException) ViewTreeObserver(android.view.ViewTreeObserver) TargetApi(android.annotation.TargetApi) Toolbar(android.support.v7.widget.Toolbar)

Aggregations

TargetApi (android.annotation.TargetApi)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 OnApplyWindowInsetsListener (android.support.v4.view.OnApplyWindowInsetsListener)1 WindowInsetsCompat (android.support.v4.view.WindowInsetsCompat)1 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)1 GridLayoutManager (android.support.v7.widget.GridLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Toolbar (android.support.v7.widget.Toolbar)1 View (android.view.View)1 ViewTreeObserver (android.view.ViewTreeObserver)1 Button (android.widget.Button)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1