Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Program lets user choose device type which then displays all
- * HardwareVendors for this device type. User can then choose
- * hardware name for chosen vendor. Then choose possible OS based
- * on the 3 previous factors. Finally a list of possible OS combinations
- * is displayed for the hardware name of the hardware vendor with
- * a chosen platform name.
- *
- * Requires premium or enterprise data. Can adopt for use with Lite but
- * with different properties.
- */
- package sttest;
- import fiftyone.mobile.detection.Provider;
- import fiftyone.mobile.detection.entities.Component;
- import fiftyone.mobile.detection.entities.Profile;
- import fiftyone.mobile.detection.entities.Property;
- import fiftyone.mobile.detection.entities.Signature;
- import fiftyone.mobile.detection.entities.Value;
- import fiftyone.mobile.detection.factories.StreamFactory;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collections;
- /**
- *
- * @author mike
- */
- public class Test {
- private static Provider p;
- private static BufferedReader br;
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws IOException {
- //Init program.
- br = new BufferedReader(new InputStreamReader(System.in));
- initDataFile();
- //Init parameters, used to fine-tune filtering at every stage.
- String deviceType = "";
- String hardwareVendor = "";
- String hardwareName = "";
- String platformName = "";
- System.out.println("***** Starting Choice Menu *****");
- for (int i = 0; i < 5; i++) {
- switch(i) {
- case 0:
- //Print all values for DeviceType
- System.out.println("DEVICE TYPES:");
- ArrayList<String> deviceTypes = getDeviceTypes();
- printArrayList(deviceTypes);
- //User chooses device type.
- System.out.print("PLEASE ENTER VALID DEVICE TYPE: ");
- //Make sure device type is valid
- do {
- deviceType = getUserInput();
- } while(!deviceTypes.contains(deviceType));
- System.out.println();
- break;
- case 1:
- //Device type selected. Now get hardware vendors based on device type.
- ArrayList<String> hardwareVendors = getHardwareVendors(deviceType);
- printArrayList(hardwareVendors);
- System.out.print("PLEASE ENTER VALID HARDVARE VENDOR: ");
- //Make sure hardware vendor is valid.
- do {
- hardwareVendor = getUserInput();
- } while(!hardwareVendors.contains(hardwareVendor));
- System.out.println();
- break;
- case 2:
- //Hardware vendor chosen, get hardware models.
- ArrayList<String> hardwareNames = getHardwareNames(hardwareVendor, deviceType);
- printArrayList(hardwareNames);
- System.out.print("PLEASE ENTER VALID HARDWARE NAME: ");
- //Make sure hardware models are valid.
- do {
- hardwareName = getUserInput();
- } while(!hardwareNames.contains(hardwareName));
- System.out.println();
- break;
- case 3:
- /* HardwareModel selected, get all platform names to do with it. */
- ArrayList<String> platformNames = getPlatformNames(hardwareName ,hardwareVendor, deviceType);
- printArrayList(platformNames);
- System.out.print("PLEASE ENTER VALID PLATFORM NAME: ");
- //Make sure platform name is valid.
- do {
- platformName = getUserInput();
- } while(!platformNames.contains(platformName));
- System.out.println();
- break;
- case 4:
- /* Finally, get all OS versions valid for that hardware and platform combination. */
- ArrayList<String> platformVersions = getPlatformVersions(platformName, hardwareName ,hardwareVendor, deviceType);
- System.out.println("The operating system"+platformName+" on "+hardwareName+" "+hardwareVendor+
- " can have the following versions:");
- Collections.sort(platformVersions);
- printArrayList(platformVersions);
- break;
- default: System.out.println("Sorry, something went wrong.");
- break;
- }
- }
- System.exit(0);
- }
- /**
- * Function initialised data file and signature ranking. Prints basic info.
- * @throws IOException
- */
- private static void initDataFile() throws IOException {
- //Initialise dataset, print basic info.
- System.out.print("Initialising provider. ");
- p = new Provider(StreamFactory.create("C:\\path\\to\\51degrees.dat"));
- System.out.println("Done.");
- System.out.println("***** Data file info *****");
- System.out.println("Data file: "+p.dataSet.getName());
- System.out.println("Published: "+p.dataSet.published);
- System.out.println("Proeprties: "+p.dataSet.getProperties().size());
- System.out.println("**************************");
- System.out.print("Initialising signatures. ");
- //NEW: initialise signatures.
- p.dataSet.initSignatureRanks();
- System.out.print("Done");
- System.out.println();
- }
- /**
- * Read in user input.
- * @return String
- */
- private static String getUserInput() throws IOException {
- return br.readLine();
- }
- /**
- * Prints an array list of strings.
- * @param toPrint ArrayList of strings.
- */
- private static void printArrayList(ArrayList<String> toPrint) {
- if(!toPrint.isEmpty()) {
- for (int i = 0; i < toPrint.size(); i++) {
- System.out.println(toPrint.get(i));
- }
- }
- }
- /**
- * Return the possible device types.
- * @return ArrayList of strings, one tor each device type.
- * @throws IOException
- */
- private static ArrayList<String> getDeviceTypes() throws IOException {
- ArrayList<String> result = new ArrayList<>();
- for (Value v : p.dataSet.get("DeviceType").getValues()) {
- result.add(v.getName());
- }
- return result;
- }
- /**
- * Returns a list of all hardware vendors for a given device type.
- * @param deviceType String to filter by device type.
- * @return ArrayList of Strings with unique hardware vendors.
- * @throws IOException
- */
- private static ArrayList<String> getHardwareVendors(String deviceType) throws IOException {
- ArrayList<String> distinctHardwareVendors = new ArrayList<>();
- //Loop through all profiles in the Hardware components.
- for (Profile pr : p.dataSet.getHardware().getProfiles()){
- //Get all values with given DeviceType parameter.
- String val = pr.getValues("DeviceType").get(0).getName();
- if(val.equals(deviceType)) {
- //Add distinct related Profiles to the list.
- ArrayList<Profile> relatedProfiles = new ArrayList<>();
- //Calculate max possible rank.
- int max = 0;
- int threshold = 75;
- for (Signature s : pr.Signatures()) {
- if (max < s.getRank())
- max = s.getRank();
- }
- //Cut off everything above the threshold.
- int maxRank = (max/100)*threshold;
- for (Signature s : pr.Signatures()) {
- if (s.getRank() < maxRank) {
- for (Profile p1 : s.getProfiles()) {
- if(!relatedProfiles.contains(p1))
- relatedProfiles.add(p1);
- }
- }
- }
- //Go through each profile in the relatedProfiles list to filter
- //by some variable.
- for (Profile p2 : relatedProfiles) {
- if(p2.getComponent().getName().equals("HardwarePlatform")) {
- String osName = p2.getValues("HardwareVendor").get(0).getName();
- if(!distinctHardwareVendors.contains(osName)) {
- distinctHardwareVendors.add(osName);
- }
- }
- }
- }
- }
- return distinctHardwareVendors;
- }
- /**
- * Function returns the list of strings derived from filtering by HardwareVendor and DeviceType
- * @param hv String representing HardwareVendor filter criteria.
- * @param dt String representing DeviceType filter criteria.
- * @return ArrayList of Strings with distinct Hardware Names.
- * @throws IOException
- */
- private static ArrayList<String> getHardwareNames(String hv, String dt) throws IOException {
- ArrayList<String> distinctHardwareNames = new ArrayList<>();
- //Loop through all profiles in the Hardware components.
- for (Profile pr : p.dataSet.getHardware().getProfiles()){
- String hardwareVendor = pr.getValues("HardwareVendor").get(0).getName();
- String deviceType = pr.getValues("DeviceType").get(0).getName();
- //Filter by hardware vendor and device type.
- if(hardwareVendor.equals(hv) && deviceType.equals(dt)) {
- //Add distinct related Profiles to the list.
- ArrayList<Profile> relatedProfiles = new ArrayList<>();
- //Calculate max possible rank to cut off some of the values.
- int max = 0;
- int threshold = 100;
- for (Signature s : pr.Signatures()) {
- if (max < s.getRank())
- max = s.getRank();
- }
- //Cut off everything above the threshold.
- int maxRank = (max/100)*threshold;
- for (Signature s : pr.Signatures()) {
- if (s.getRank() < maxRank) {
- for (Profile p1 : s.getProfiles()) {
- if(!relatedProfiles.contains(p1))
- relatedProfiles.add(p1);
- }
- }
- }
- //Go through each profile in the relatedProfiles list to filter
- //by some variable.
- for (Profile p2 : relatedProfiles) {
- if(p2.getComponent().getName().equals("HardwarePlatform")) {
- String resultHardwareName = "";
- for (int i = 0; i < p2.getValues("HardwareName").size(); i++) {
- resultHardwareName = p2.getValues("HardwareName").get(i).getName();
- //Add unique browser name to the list to be dispalyed.
- if(!distinctHardwareNames.contains(resultHardwareName)) {
- distinctHardwareNames.add(resultHardwareName);
- }
- }
- }
- }
- }
- }
- return distinctHardwareNames;
- }
- /**
- * Function returns the list of platform names based on HardwareName, HardwareVendor and DeviceType.
- * @param hn String HardwareName filter
- * @param hv String HardwareVendor filter.
- * @param dt String DeviceType filter.
- * @return ArrayList of Strings with PlatformName values
- * @throws IOException
- */
- private static ArrayList<String> getPlatformNames(String hn, String hv, String dt) throws IOException {
- ArrayList<String> distinctPlatformNames = new ArrayList<>();
- //Loop through all profiles in the Hardware components.
- for (Profile pr : p.dataSet.getHardware().getProfiles()){
- String hardwareName = pr.getValues("HardwareName").get(0).getName();
- String hardwareVendor = pr.getValues("HardwareVendor").get(0).getName();
- String deviceType = pr.getValues("DeviceType").get(0).getName();
- //Find all for chosen devie type.
- if(hardwareName.equals(hn) && hardwareVendor.equals(hv) && deviceType.equals(dt)) {
- //DEBUG: System.out.println(hn+" "+hv+" "+dt);
- //Add distinct related Profiles to the list.
- ArrayList<Profile> relatedProfiles = new ArrayList<>();
- //Calculate max possible rank to cut off some of the values.
- int max = 0;
- int threshold = 100;
- for (Signature s : pr.Signatures()) {
- if (max < s.getRank())
- max = s.getRank();
- }
- //Cut off everything above the threshold.
- int maxRank = (max/100)*threshold;
- for (Signature s : pr.Signatures()) {
- if (hn.startsWith("iPhone")) {
- for (Profile p1 : s.getProfiles()) {
- if(!relatedProfiles.contains(p1))
- relatedProfiles.add(p1);
- }
- } else {
- if (s.getRank() <= maxRank) {
- //System.out.println("Test");
- for (Profile p1 : s.getProfiles()) {
- if(!relatedProfiles.contains(p1))
- relatedProfiles.add(p1);
- }
- }
- }
- }
- //Go through each profile in the relatedProfiles list to filter
- //by some variable.
- for (Profile p2 : relatedProfiles) {
- if(p2.getComponent().getName().equals("SoftwarePlatform")) {
- String tempPlatformName = p2.getValues("PlatformName").get(0).getName();
- //Add unique browser name to the list to be dispalyed.
- if(!distinctPlatformNames.contains(tempPlatformName)) {
- distinctPlatformNames.add(tempPlatformName);
- }
- }
- }
- }
- }
- return distinctPlatformNames;
- }
- /**
- * Returns a list of Platform Names for HardwareVendor's HardwareValue running a specific PlatformName OS.
- * @param pn String PlatformName
- * @param hn String HardwareName
- * @param hv String HardwareVendor
- * @param dt String DeviceType
- * @return Returns a list of Strings with OS versions
- * @throws IOException
- */
- private static ArrayList<String> getPlatformVersions(String pn, String hn, String hv, String dt) throws IOException {
- ArrayList<String> distinctPlatformVersions = new ArrayList<>();
- //Loop through all profiles in the Hardware components.
- for (Profile pr : p.dataSet.getHardware().getProfiles()){
- String hardwareName = pr.getValues("HardwareName").get(0).getName();
- String hardwareVendor = pr.getValues("HardwareVendor").get(0).getName();
- String deviceType = pr.getValues("DeviceType").get(0).getName();
- if(hardwareName.equals(hn) && hardwareVendor.equals(hv) && deviceType.equals(dt)) {
- //Add distinct related Profiles to the list.
- ArrayList<Profile> relatedProfiles = new ArrayList<>();
- for (Signature s : pr.Signatures()) {
- //if (s.getRank() < maxRank) {
- for (Profile p1 : s.getProfiles()) {
- if(!relatedProfiles.contains(p1))
- relatedProfiles.add(p1);
- }
- //}
- }
- //Go through each profile in the relatedProfiles list to filter
- //by some variable.
- for (Profile p2 : relatedProfiles) {
- if(p2.getComponent().getName().equals("SoftwarePlatform")) {
- String osName = p2.getValues("PlatformName").get(0).getName();
- //Now, check that name of the OS corresponds to the one we want.
- if(osName.equals(pn)) {
- String tempPlatformVersion = p2.getValues("PlatformVersion").get(0).getName();
- //Add unique browser name to the list to be dispalyed.
- if(!distinctPlatformVersions.contains(tempPlatformVersion)) {
- distinctPlatformVersions.add(tempPlatformVersion);
- }
- }
- }
- }
- }
- }
- return distinctPlatformVersions;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment