Skip to content

Instantly share code, notes, and snippets.

@adorazhang
adorazhang / SwipeDismissBehavior.java
Created September 29, 2018 01:43
This will work for both directions.
@Override
public void onViewPositionChanged(View child, int x, int y, int dx, int dy) {
float startAlphaDistance = mOriginalCapturedViewLeft
+ child.getWidth() * mAlphaStartSwipeDistance;
float endAlphaDistance = mOriginalCapturedViewLeft
+ child.getWidth() * mAlphaEndSwipeDistance;
float diff = Math.abs(x - startAlphaDistance);
float totalLength = endAlphaDistance - startAlphaDistance;
ViewCompat.setAlpha(child, clamp(0f, 1f - diff / totalLength, 1f));
@adorazhang
adorazhang / SwipeDismissBehavior.java
Created September 29, 2018 01:39
The original onViewPositionChanged() in SwipeDismissBehavior.
private final ViewDragHelper.Callback dragCallback =
new ViewDragHelper.Callback() {
...
@Override
public void onViewPositionChanged(View child, int left, int top, int dx, int dy) {
final float startAlphaDistance =
originalCapturedViewLeft + child.getWidth() * alphaStartSwipeDistance;
final float endAlphaDistance =
// The following is pseudo code. If you want actual code:
// https://android.googlesource.com/platform/frameworks/base/+/master/packages/SystemUI/src/com/android/systemui/statusbar/NotificationData.java
private RankingMap mRankingMap;
Ranking mTemporaryRanking = new Ranking();
@Override
public void onNotificationRankingUpdate(RankingMap rankingMap) {
mRankingMap = rankingMap;
for (StatusBarNotification sbn : mYourNotifications) {
<service android:name=".ExampleNotificationListener"
android:label="@string/notification_service_label"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:directBootAware="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
for (int i = 0; i < nUserIds; ++i) {
final Set<ComponentName> add = toAdd.get(userIds[i]);
for (ComponentName component : add) {
try {
ServiceInfo info = mPm.getServiceInfo(component,
PackageManager.MATCH_DIRECT_BOOT_AWARE
| PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userIds[i]);
if (info == null || !mConfig.bindPermission.equals(info.permission)) {
Slog.w(TAG, "Not binding " + getCaption() + " service " + component
+ ": it does not require the permission " + mConfig.bindPermission);
@adorazhang
adorazhang / 8Queens.java
Created March 16, 2016 22:21
8 Queens Solution for Shijia
import java.lang.Math;
public class HelloWorld {
public static void main(String[] args) {
Board b = new Board(8, 8);
b.findSolution(1);
b.Print();
}
}
public class Solution {
public bool WordBreak(string s, ISet<string> wordDict) {
int len = s.Length;
bool[] canBreak = new bool[len + 1];
canBreak[0] = true;
for(int i = 1; i <= len; i++) { // i stands for the length of the substring
foreach (string word in wordDict) {
if (i - word.Length < 0) continue;
if (canBreak[i - word.Length] && word == s.Substring(i - word.Length, word.Length)) {
canBreak[i] = true;
@adorazhang
adorazhang / binary-tree-maximum-path-sum.cs
Created December 7, 2015 08:08
C# Solution to Leetcode Binary Tree Maximum Path Sum Problem
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@adorazhang
adorazhang / GameOfLifeNoReturn.cs
Last active November 15, 2015 00:35
Conway's Game of Life in C# with a constraint that no functions can have a return value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameOfLife
{
class Program
{
@adorazhang
adorazhang / GameOfLife3D.cs
Created November 14, 2015 23:41
3D Version of Conway's Game of Life Implementation in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameOfLife3D
{
class Program
{