1/*
2 * Copyright (C) 2016 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
17package com.android.internal.util;
18
19import com.android.layoutlib.bridge.impl.DelegateManager;
20import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
21
22import android.util.LongSparseLongArray;
23
24/**
25 * Delegate used to provide new implementation the native methods of {@link VirtualRefBasePtr}
26 *
27 * Through the layoutlib_create tool, the original native  methods of VirtualRefBasePtr have been
28 * replaced by calls to methods of the same name in this delegate class.
29 *
30 */
31@SuppressWarnings("unused")
32public class VirtualRefBasePtr_Delegate {
33    private static final DelegateManager<Object> sManager = new DelegateManager<>(Object.class);
34    private static final LongSparseLongArray sRefCount = new LongSparseLongArray();
35
36    @LayoutlibDelegate
37    /*package*/ static synchronized void nIncStrong(long ptr) {
38        long counter = sRefCount.get(ptr);
39        sRefCount.put(ptr, ++counter);
40    }
41
42    @LayoutlibDelegate
43    /*package*/ static synchronized void nDecStrong(long ptr) {
44        long counter = sRefCount.get(ptr);
45
46        if (counter > 1) {
47            sRefCount.put(ptr, --counter);
48        } else {
49            sRefCount.delete(ptr);
50            sManager.removeJavaReferenceFor(ptr);
51        }
52    }
53}
54