aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/anysoftkeyboard/addons/AddOnImpl.java
blob: 5c612fd5cd5b1d01c1ada8ed566bcf28f83194e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (c) 2013 Menny Even-Danan
 *
 * 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.anysoftkeyboard.addons;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.SparseArrayCompat;
import android.util.SparseIntArray;

import com.anysoftkeyboard.utils.Log;

import java.lang.ref.WeakReference;
import java.util.Arrays;

public abstract class AddOnImpl implements AddOn {

    private static final String TAG = "ASK_AddOnImpl";
    private final String mId;
    private final String mName;
    private final String mDescription;
    private final String mPackageName;
    private final Context mAskAppContext;
    private WeakReference<Context> mPackageContext;
    private final int mSortIndex;
    private final AddOnResourceMappingImpl mAddOnResourceMapping;

    protected AddOnImpl(Context askContext, Context packageContext, String id, int nameResId,
                        String description, int sortIndex) {
        mId = id;
        mAskAppContext = askContext;
        mName = packageContext.getString(nameResId);
        mDescription = description;
        mPackageName = packageContext.getPackageName();
        mPackageContext = new WeakReference<>(packageContext);
        mSortIndex = sortIndex;
        mAddOnResourceMapping = new AddOnResourceMappingImpl(this);
    }

    public final String getId() {
        return mId;
    }

    public final String getDescription() {
        return mDescription;
    }

    public String getPackageName() {
        return mPackageName;
    }

    @Nullable
    public final Context getPackageContext() {
        Context c = mPackageContext.get();
        if (c == null) {
            try {
                c = mAskAppContext.createPackageContext(mPackageName, Context.CONTEXT_IGNORE_SECURITY);
                mPackageContext = new WeakReference<>(c);
            } catch (NameNotFoundException e) {
                Log.w(TAG, "Failed to find package %s!", mPackageName);
                Log.w(TAG, "Failed to find package! ", e);
            }
        }
        return c;
    }

    public final int getSortIndex() {
        return mSortIndex;
    }

    public String getName() {
        return mName;
    }

    @Override
    public int hashCode() {
        return getId().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        return o instanceof AddOn &&
                ((AddOn) o).getId().equals(getId());
    }

    @NonNull
    @Override
    public AddOnResourceMapping getResourceMapping() {
        return mAddOnResourceMapping;
    }

    private static class AddOnResourceMappingImpl implements AddOnResourceMapping {
        private final WeakReference<AddOnImpl> mAddOnWeakReference;
        private final SparseIntArray mAttributesMapping = new SparseIntArray();
        private final SparseArrayCompat<int[]> mStyleableArrayMapping = new SparseArrayCompat<>();

        private AddOnResourceMappingImpl(@NonNull AddOnImpl addOn) {
            mAddOnWeakReference = new WeakReference<>(addOn);
        }

/*        @AttrRes
        @Override
        public int getLocalAttrIdFromRemote(@AttrRes int remoteAttributeResourceId) {
            //8.6
            int indexOfLocalAttrId = mAttributesMapping.indexOfKey(remoteAttributeResourceId);
            if (indexOfLocalAttrId >= 0) return mAttributesMapping.valueAt(indexOfLocalAttrId);
            AddOnImpl addOn = mAddOnWeakReference.get();
            if (addOn == null) return 0;
            Context askContext = addOn.mAskAppContext;
            Context remoteContext = addOn.getPackageContext();
            if (remoteContext == null) return 0;
            int[] remoteAttrIds = Support.createBackwardCompatibleStyleable(new int[]{localAttributeResourceId}, askContext, remoteContext, mAttributesMapping);
            mAttributesMapping.put(localAttributeResourceId, remoteAttrIds[0]);
            return remoteAttrIds[0];
        }*/

        @Override
        public int[] getRemoteStyleableArrayFromLocal(int[] localStyleableArray) {
            int localStyleableId = Arrays.hashCode(localStyleableArray);
            int indexOfRemoteArray = mStyleableArrayMapping.indexOfKey(localStyleableId);
            if (indexOfRemoteArray >= 0) return mStyleableArrayMapping.valueAt(indexOfRemoteArray);
            AddOnImpl addOn = mAddOnWeakReference.get();
            if (addOn == null) return new int[0];
            Context askContext = addOn.mAskAppContext;
            Context remoteContext = addOn.getPackageContext();
            if (remoteContext == null) return new int[0];
            int[] remoteAttrIds = Support.createBackwardCompatibleStyleable(localStyleableArray, askContext, remoteContext, mAttributesMapping);
            mStyleableArrayMapping.put(localStyleableId, remoteAttrIds);
            return remoteAttrIds;
        }
    }
}