init
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/* Copyright (c) 2021, Arm Limited and Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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.khronos.vulkan_samples.model;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
public class Permission {
|
||||
private final String name;
|
||||
private final int code;
|
||||
private boolean requested = false;
|
||||
|
||||
public Permission(String name, int code) {
|
||||
this.name = name;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the requested state of the permission
|
||||
*
|
||||
* @param requested The desired requested state
|
||||
*/
|
||||
public void setRequested(boolean requested) {
|
||||
this.requested = requested;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query if the permission has already been requested
|
||||
*
|
||||
* @return True if previously requested; false otherwise
|
||||
*/
|
||||
public boolean isRequested() {
|
||||
return requested;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the permission
|
||||
*
|
||||
* @return The permissions name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the code of the permission
|
||||
*
|
||||
* @return The permissions code
|
||||
*/
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query whether the permission has been granted for the App
|
||||
*
|
||||
* @param ctx The activity context that needs to be queried for the permission
|
||||
* @return True if permission granted; false otherwise
|
||||
*/
|
||||
public boolean granted(Context ctx) {
|
||||
return ContextCompat.checkSelfPermission(ctx, name) == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request the permission for a given Activity
|
||||
*
|
||||
* @param activity Activity to request the permission for
|
||||
*/
|
||||
public void request(Activity activity) {
|
||||
ActivityCompat.requestPermissions(activity, new String[]{name}, code);
|
||||
requested = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/* Copyright (c) 2019-2021, Arm Limited and Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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.khronos.vulkan_samples.model;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.khronos.vulkan_samples.common.Utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Sample implements Comparable<Sample> {
|
||||
private final String id;
|
||||
private final String category;
|
||||
private final String author;
|
||||
private final String name;
|
||||
private final String description;
|
||||
|
||||
private String tagText;
|
||||
private final List<String> tags;
|
||||
|
||||
public Sample(String id, String category, String author, String name, String description, String[] tags) {
|
||||
this.id = id;
|
||||
this.category = category;
|
||||
this.author = author;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
|
||||
this.tags = new LinkedList<>(Arrays.asList(tags));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the Tag Text that is shown in the sample list
|
||||
*
|
||||
* @return A concatenated string of a samples tags
|
||||
*/
|
||||
private String generateTagText() {
|
||||
List<String> formatted_tags = new ArrayList<>(this.tags.size());
|
||||
|
||||
for (String tag : this.tags) {
|
||||
formatted_tags.add(Utils.capitalize(tag));
|
||||
}
|
||||
|
||||
// If the sample has a tag other than "Any" then "Any" can be removed
|
||||
if (formatted_tags.size() > 1) {
|
||||
formatted_tags.remove("Any");
|
||||
}
|
||||
|
||||
return TextUtils.join(", ", formatted_tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Sample's ID
|
||||
*
|
||||
* @return A Sample ID
|
||||
*/
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sample's category
|
||||
*
|
||||
* @return The sample's category
|
||||
*/
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sample's author
|
||||
*
|
||||
* @return The sample's author
|
||||
*/
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sample's name
|
||||
*
|
||||
* @return The sample's name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sample's description
|
||||
*
|
||||
* @return The sample's description
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sample's tags
|
||||
*
|
||||
* @return The sample's tags
|
||||
*/
|
||||
public List<String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sample's tag text
|
||||
*
|
||||
* @return The sample's tag text
|
||||
*/
|
||||
public String getTagText() {
|
||||
if (tagText == null) {
|
||||
tagText = generateTagText();
|
||||
}
|
||||
return tagText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether two samples are equal
|
||||
* Can be used with a HashMap
|
||||
*
|
||||
* @return True if samples are equal; false otherwise
|
||||
*/
|
||||
public boolean equals(Object object) {
|
||||
if (this == object) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (object instanceof Sample) {
|
||||
Sample sample = (Sample) object;
|
||||
return this.id.equals(sample.id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used when comparing two samples - Alphabetical Case Insensitive
|
||||
* Sample id's are unique so this is used to compare
|
||||
*
|
||||
* @param sample To compare too
|
||||
* @return - if less, 0 if equal + if greater than
|
||||
*/
|
||||
public int compareTo(Sample sample) {
|
||||
return sample.id.compareToIgnoreCase(this.id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
/* Copyright (c) 2021, Arm Limited and Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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.khronos.vulkan_samples.model;
|
||||
|
||||
import com.khronos.vulkan_samples.common.Utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* A one stop store of samples that are pre indexed for use in the App
|
||||
*/
|
||||
public class SampleStore {
|
||||
private final Set<Sample> samples = new TreeSet<>();
|
||||
|
||||
private final Set<String> categories = new TreeSet<>(new Utils.PredefinedOrderComparator("api", "performance", "extensions"));
|
||||
private final Map<String, List<Sample>> categoryIndex = new HashMap<>();
|
||||
|
||||
private final Set<String> tags = new TreeSet<>(new Utils.PredefinedOrderComparator("any"));
|
||||
private final Map<String, List<Sample>> tagIndex = new HashMap<>();
|
||||
|
||||
public SampleStore(List<Sample> samples) {
|
||||
if (samples == null) {
|
||||
samples = new ArrayList<>();
|
||||
}
|
||||
|
||||
// Index all samples by tag and by category
|
||||
for (Sample sample : samples) {
|
||||
if (sample == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.samples.add(sample);
|
||||
|
||||
if (!categoryIndex.containsKey(sample.getCategory())) {
|
||||
categoryIndex.put(sample.getCategory(), new ArrayList<>());
|
||||
}
|
||||
|
||||
List<Sample> category = categoryIndex.get(sample.getCategory());
|
||||
if (category != null) {
|
||||
category.add(sample);
|
||||
}
|
||||
|
||||
for (String tag : sample.getTags()) {
|
||||
if (!tagIndex.containsKey(tag)) {
|
||||
tagIndex.put(tag, new ArrayList<>());
|
||||
}
|
||||
|
||||
List<Sample> index = tagIndex.get(tag);
|
||||
if (index != null) {
|
||||
index.add(sample);
|
||||
}
|
||||
|
||||
this.tags.add(tag);
|
||||
}
|
||||
|
||||
this.categories.add(sample.getCategory());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a sample by its id
|
||||
*
|
||||
* @param id of a given sample
|
||||
* @return A sample if it is found
|
||||
*/
|
||||
public Sample findByID(String id) {
|
||||
for (Sample sample : samples) {
|
||||
if (sample.getId().equals(id)) {
|
||||
return sample;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the category index
|
||||
*
|
||||
* @return A map of category to samples
|
||||
*/
|
||||
public Map<String, List<Sample>> getCategoryIndex() {
|
||||
return this.categoryIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all category names
|
||||
*
|
||||
* @return A list of category names
|
||||
*/
|
||||
public List<String> getCategories() {
|
||||
return new ArrayList<>(this.categories);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tag names
|
||||
*
|
||||
* @return A list of tag names
|
||||
*/
|
||||
public List<String> getTags() {
|
||||
return new ArrayList<>(this.tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of sample by tag name
|
||||
*
|
||||
* @return A list of samples
|
||||
*/
|
||||
public List<Sample> getByTag(String tag) {
|
||||
return this.tagIndex.get(tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of sample by multiple tag names
|
||||
*
|
||||
* @return A list of samples
|
||||
*/
|
||||
public List<Sample> getByTags(List<String> tags) {
|
||||
Set<Sample> samples = new HashSet<>();
|
||||
|
||||
for (String tag : tags) {
|
||||
Collection<Sample> samplesByTag = getByTag(tag);
|
||||
if (samplesByTag != null) {
|
||||
samples.addAll(samplesByTag);
|
||||
}
|
||||
}
|
||||
|
||||
return new ArrayList<>(samples);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of sample by category name
|
||||
*
|
||||
* @return A list of samples
|
||||
*/
|
||||
public List<Sample> getByCategory(String category) {
|
||||
return this.categoryIndex.get(category);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user