diff options
author | Benjamin Baxter <benbaxter@google.com> | 2017-03-22 15:54:37 -0700 |
---|---|---|
committer | Benjamin Baxter <benbaxter@google.com> | 2017-04-03 17:10:22 -0700 |
commit | 06feed786f96cb11cb374645efadcec5b8a900d3 (patch) | |
tree | a1d652b0ce11958944cc05d32f4d06cff2c100a1 /wearable/wear/WearMessagingApp | |
parent | b60bee2a110fb2752e9a45c4102b1bb63a5e48de (diff) | |
download | android-06feed786f96cb11cb374645efadcec5b8a900d3.tar.gz |
Adding util classes copied from SUP
Bug: 34841755
Change-Id: I8d93a032a8cc18f808a3d4c4d8b7b6b80a799c01
Diffstat (limited to 'wearable/wear/WearMessagingApp')
3 files changed, 152 insertions, 0 deletions
diff --git a/wearable/wear/WearMessagingApp/Wearable/src/main/java/com/example/android/wearable/wear/messaging/util/DividerItemDecoration.java b/wearable/wear/WearMessagingApp/Wearable/src/main/java/com/example/android/wearable/wear/messaging/util/DividerItemDecoration.java new file mode 100644 index 00000000..7ea10de0 --- /dev/null +++ b/wearable/wear/WearMessagingApp/Wearable/src/main/java/com/example/android/wearable/wear/messaging/util/DividerItemDecoration.java @@ -0,0 +1,53 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.wearable.wear.messaging.util; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.drawable.Drawable; +import android.support.v4.content.ContextCompat; +import android.support.v7.widget.RecyclerView; +import android.view.View; + +/** + * Recycler view divider. + */ +public class DividerItemDecoration extends RecyclerView.ItemDecoration { + + private Drawable mDrawable; + + public DividerItemDecoration(Context context, int resId) { + mDrawable = ContextCompat.getDrawable(context, resId); + } + + @Override + public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { + int left = parent.getLeft(); + int right = parent.getRight(); + + int childCount = parent.getChildCount(); + + for (int i = 0; i < childCount; i++) { + View child = parent.getChildAt(i); + + int top = child.getBottom(); + int bottom = top + mDrawable.getIntrinsicHeight(); + + mDrawable.setBounds(left, top, right, bottom); + mDrawable.draw(c); + } + } +} diff --git a/wearable/wear/WearMessagingApp/Wearable/src/main/java/com/example/android/wearable/wear/messaging/util/MenuTinter.java b/wearable/wear/WearMessagingApp/Wearable/src/main/java/com/example/android/wearable/wear/messaging/util/MenuTinter.java new file mode 100644 index 00000000..bad1df3a --- /dev/null +++ b/wearable/wear/WearMessagingApp/Wearable/src/main/java/com/example/android/wearable/wear/messaging/util/MenuTinter.java @@ -0,0 +1,50 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.wearable.wear.messaging.util; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.support.v4.content.ContextCompat; +import android.support.v4.graphics.drawable.DrawableCompat; +import android.view.Menu; +import android.view.MenuItem; + +/** + * Helper method to tint menu item icons. + */ +public class MenuTinter { + private MenuTinter() {} + + /** + * Tints the menu icons + * + * @param context resource context + * @param menu menu to tint + * @param refColor color to tint + */ + public static void tintMenu(Context context, Menu menu, int refColor) { + for (int i = 0; i < menu.size(); i++) { + MenuItem item = menu.getItem(i); + Drawable drawable = item.getIcon(); + if (drawable != null) { + drawable = DrawableCompat.wrap(drawable); + drawable.mutate(); + DrawableCompat.setTint(drawable, ContextCompat.getColor(context, refColor)); + item.setIcon(drawable); + } + } + } +} diff --git a/wearable/wear/WearMessagingApp/Wearable/src/main/java/com/example/android/wearable/wear/messaging/util/PrescrollToBottom.java b/wearable/wear/WearMessagingApp/Wearable/src/main/java/com/example/android/wearable/wear/messaging/util/PrescrollToBottom.java new file mode 100644 index 00000000..3b98c5bc --- /dev/null +++ b/wearable/wear/WearMessagingApp/Wearable/src/main/java/com/example/android/wearable/wear/messaging/util/PrescrollToBottom.java @@ -0,0 +1,49 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.wearable.wear.messaging.util; + +import android.support.v7.widget.RecyclerView; +import android.util.Log; +import android.view.ViewTreeObserver; + +/** + * Prescrolls to the last element in the adapter before draw and removes itself. + */ +public class PrescrollToBottom implements ViewTreeObserver.OnPreDrawListener { + + private static final String TAG = "PrescrollToBottom"; + + private RecyclerView mRecyclerView; + private RecyclerView.Adapter mAdapter; + + public PrescrollToBottom(RecyclerView recyclerView, RecyclerView.Adapter adapter) { + mRecyclerView = recyclerView; + mAdapter = adapter; + } + + @Override + public boolean onPreDraw() { + boolean shouldScroll = mAdapter.getItemCount() > 2; + if (shouldScroll) { + Log.d(TAG, "Smooth scrolling after draw to position " + mAdapter.getItemCount()); + mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount()); + } + // always remove the listener so that we do not get multiple + // instances of it attached when it is not needed + mRecyclerView.getViewTreeObserver().removeOnPreDrawListener(this); + return !shouldScroll; + } +}
\ No newline at end of file |