1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.statusbar; 18 19 import android.os.Message; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.util.Log; 23 24 import java.util.ArrayDeque; 25 import java.util.Collection; 26 27 public class NotificationVisibility implements Parcelable { 28 private static final String TAG = "NoViz"; 29 private static final int MAX_POOL_SIZE = 25; 30 private static ArrayDeque<NotificationVisibility> sPool = new ArrayDeque<>(MAX_POOL_SIZE); 31 private static int sNexrId = 0; 32 33 public String key; 34 public int rank; 35 public boolean visible = true; 36 /*package*/ int id; 37 38 private NotificationVisibility() { 39 id = sNexrId++; 40 } 41 42 private NotificationVisibility(String key, int rank, boolean visibile) { 43 this(); 44 this.key = key; 45 this.rank = rank; 46 this.visible = visibile; 47 } 48 49 @Override 50 public String toString() { 51 return "NotificationVisibility(id=" + id 52 + "key=" + key 53 + " rank=" + rank 54 + (visible?" visible":"") 55 + " )"; 56 } 57 58 @Override 59 public NotificationVisibility clone() { 60 return obtain(this.key, this.rank, this.visible); 61 } 62 63 @Override 64 public int hashCode() { 65 // allow lookups by key, which _should_ never be null. 66 return key == null ? 0 : key.hashCode(); 67 } 68 69 @Override 70 public boolean equals(Object that) { 71 // allow lookups by key, which _should_ never be null. 72 if (that instanceof NotificationVisibility) { 73 NotificationVisibility thatViz = (NotificationVisibility) that; 74 return (key == null && thatViz.key == null) || key.equals(thatViz.key); 75 } 76 return false; 77 } 78 79 @Override 80 public int describeContents() { 81 return 0; 82 } 83 84 @Override 85 public void writeToParcel(Parcel out, int flags) { 86 out.writeString(this.key); 87 out.writeInt(this.rank); 88 out.writeInt(this.visible ? 1 : 0); 89 } 90 91 private void readFromParcel(Parcel in) { 92 this.key = in.readString(); 93 this.rank = in.readInt(); 94 this.visible = in.readInt() != 0; 95 } 96 97 /** 98 * Return a new NotificationVisibility instance from the global pool. Allows us to 99 * avoid allocating new objects in many cases. 100 */ 101 public static NotificationVisibility obtain(String key, int rank, boolean visible) { 102 NotificationVisibility vo = obtain(); 103 vo.key = key; 104 vo.rank = rank; 105 vo.visible = visible; 106 return vo; 107 } 108 109 private static NotificationVisibility obtain(Parcel in) { 110 NotificationVisibility vo = obtain(); 111 vo.readFromParcel(in); 112 return vo; 113 } 114 115 private static NotificationVisibility obtain() { 116 synchronized (sPool) { 117 if (!sPool.isEmpty()) { 118 return sPool.poll(); 119 } 120 } 121 return new NotificationVisibility(); 122 } 123 124 /** 125 * Return a NotificationVisibility instance to the global pool. 126 * <p> 127 * You MUST NOT touch the NotificationVisibility after calling this function because it has 128 * effectively been freed. 129 * </p> 130 */ 131 public void recycle() { 132 if (key == null) { 133 // do nothing on multiple recycles 134 return; 135 } 136 key = null; 137 if (sPool.size() < MAX_POOL_SIZE) { 138 synchronized (sPool) { 139 sPool.offer(this); 140 } 141 } 142 } 143 144 /** 145 * Parcelable.Creator that instantiates NotificationVisibility objects 146 */ 147 public static final Parcelable.Creator<NotificationVisibility> CREATOR 148 = new Parcelable.Creator<NotificationVisibility>() 149 { 150 public NotificationVisibility createFromParcel(Parcel parcel) 151 { 152 return obtain(parcel); 153 } 154 155 public NotificationVisibility[] newArray(int size) 156 { 157 return new NotificationVisibility[size]; 158 } 159 }; 160 } 161 162