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);
}
}
});
}
Aggregations