|
1 | 1 | package io.cryptolens.methods; |
2 | 2 |
|
3 | 3 | import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonArray; |
| 5 | +import com.google.gson.JsonParser; |
4 | 6 | import com.google.gson.reflect.TypeToken; |
5 | 7 | import io.cryptolens.internal.BasicResult; |
6 | 8 | import io.cryptolens.models.ActivatedMachine; |
@@ -199,22 +201,58 @@ public static boolean HasFeature(LicenseKey licenseKey, int feature) { |
199 | 201 | return false; |
200 | 202 | } |
201 | 203 |
|
| 204 | + public class MyClass<T> |
| 205 | + { |
| 206 | + //public String par1; |
| 207 | + public T par2; |
| 208 | + } |
| 209 | + |
202 | 210 | /** |
203 | | - * Use the notes field to determine if a certain feature exists (instead of the 8 feature flags). |
204 | | - * The notes field needs to be formatted as JSON array, eg. ["f1", "f2"] means f1 and f2 are true. |
| 211 | + * <p>Uses the notes field to determine if a certain feature exists (instead of the 8 feature flags).</p> |
| 212 | + * <p><strong>Formatting: </strong> <p>The notes field needs to be formatted as a JSON array of strings or objects that contain </p> |
| 213 | + * For example, ["f1", "f2"] means f1 and f2 are true. You can also have feature bundling, eg. ["f1", {"f2": ["voice","image"]}], |
| 214 | + * which means that f1 and f2 are true, as well as f2.limited and f2.image. You can set any depth, eg. you can have |
| 215 | + * ["f1", {"f2":[{"voice":["all"]}, "image"]}] means f2.voice.all is true as well as f2.voice and f2. |
| 216 | + * The dots symbol is used to specify the "sub-features". |
| 217 | + * </p> |
205 | 218 | * @param licenseKey a license key object. |
206 | 219 | * @param featureName the name of the feature (case-sensitive). |
207 | 220 | * @return True if the feature exists and false otherwise. |
208 | 221 | */ |
209 | 222 | public static boolean HasFeature(LicenseKey licenseKey, String featureName) { |
210 | 223 |
|
211 | | - Type type = new TypeToken<HashSet<String>>(){}.getType(); |
212 | | - HashSet<String> features = new Gson().fromJson(licenseKey.Notes, type); |
| 224 | + JsonParser parser = new JsonParser(); |
| 225 | + JsonArray array = parser.parse(licenseKey.Notes).getAsJsonArray(); |
| 226 | + String[] featurePath = featureName.split("\\."); |
| 227 | + |
| 228 | + boolean found = false; |
| 229 | + for(int i = 0; i < featurePath.length; i++) { |
| 230 | + int index = -1; |
| 231 | + for(int j = 0; j < array.size(); j++) { |
| 232 | + |
| 233 | + if(!array.get(j).isJsonObject() && array.get(j).getAsString().equals(featurePath[i])) { |
| 234 | + found = true; |
| 235 | + break; |
| 236 | + } else if (array.get(j).isJsonObject() && array.get(j).getAsJsonObject().keySet().contains(featurePath[i])){ |
| 237 | + found = true; |
| 238 | + index = j; |
| 239 | + break; |
| 240 | + } |
| 241 | + } |
| 242 | + if(!found){ |
| 243 | + return false; |
| 244 | + } |
| 245 | + if(i + 1 < featurePath.length && index != -1) { |
| 246 | + // still have some sub features to go through. |
| 247 | + array = array.get(index).getAsJsonObject().get(featurePath[i]).getAsJsonArray(); |
| 248 | + found = false; |
| 249 | + } |
| 250 | + } |
213 | 251 |
|
214 | | - if (features != null && features.contains(featureName)) { |
215 | | - return true; |
| 252 | + if(!found){ |
| 253 | + return false; |
216 | 254 | } |
217 | | - return false; |
| 255 | + return true; |
218 | 256 | } |
219 | 257 |
|
220 | 258 | /** |
|
0 commit comments