Search in sources :

Example 1 with Library

use of com.mikepenz.aboutlibraries.entity.Library in project AboutLibraries by mikepenz.

the class Libs method getAutoDetectedLibraries.

/**
     * Get all autoDetected Libraries
     *
     * @return an ArrayList Library with all found libs by their classpath
     */
public ArrayList<Library> getAutoDetectedLibraries(Context ctx) {
    ArrayList<Library> libraries = new ArrayList<>();
    PackageInfo pi = Util.getPackageInfo(ctx);
    if (pi != null) {
        String[] autoDetectedLibraries = ctx.getSharedPreferences("aboutLibraries_" + pi.versionCode, Context.MODE_PRIVATE).getString("autoDetectedLibraries", "").split(";");
        if (autoDetectedLibraries.length > 0) {
            for (String autoDetectedLibrary : autoDetectedLibraries) {
                Library lib = getLibrary(autoDetectedLibrary);
                if (lib != null) {
                    libraries.add(lib);
                }
            }
        }
    }
    if (libraries.size() == 0) {
        String delimiter = "";
        String autoDetectedLibrariesPref = "";
        for (Library lib : Detect.detect(ctx, getLibraries())) {
            libraries.add(lib);
            autoDetectedLibrariesPref = autoDetectedLibrariesPref + delimiter + lib.getDefinedName();
            delimiter = ";";
        }
        if (pi != null) {
            ctx.getSharedPreferences("aboutLibraries_" + pi.versionCode, Context.MODE_PRIVATE).edit().putString("autoDetectedLibraries", autoDetectedLibrariesPref).commit();
        }
    }
    return libraries;
}
Also used : PackageInfo(android.content.pm.PackageInfo) ArrayList(java.util.ArrayList) Library(com.mikepenz.aboutlibraries.entity.Library)

Example 2 with Library

use of com.mikepenz.aboutlibraries.entity.Library in project AboutLibraries by mikepenz.

the class Libs method getAutoDetectedLibraries.

/**
 * Get all autoDetected Libraries
 *
 * @param ctx                  the current context
 * @param checkCachedDetection defines if we should check the cached autodetected libraries (per version) (default: enabled)
 * @return an ArrayList Library with all found libs by their classpath
 */
public List<Library> getAutoDetectedLibraries(Context ctx, boolean checkCachedDetection) {
    List<Library> libraries;
    PackageInfo pi = Util.getPackageInfo(ctx);
    SharedPreferences sharedPreferences = ctx.getSharedPreferences("aboutLibraries", Context.MODE_PRIVATE);
    int lastCacheVersion = sharedPreferences.getInt("versionCode", -1);
    boolean isCacheUpToDate = pi != null && lastCacheVersion == pi.versionCode;
    if (checkCachedDetection) {
        // Retrieve from cache if up to date
        if (pi != null && isCacheUpToDate) {
            String[] autoDetectedLibraries = sharedPreferences.getString("autoDetectedLibraries", "").split(DELIMITER);
            if (autoDetectedLibraries.length > 0) {
                libraries = new ArrayList<>(autoDetectedLibraries.length);
                for (String autoDetectedLibrary : autoDetectedLibraries) {
                    Library lib = getLibrary(autoDetectedLibrary);
                    if (lib != null)
                        libraries.add(lib);
                }
                return libraries;
            }
        }
    }
    libraries = Detect.detect(ctx, getLibraries());
    if (pi != null && !isCacheUpToDate) {
        // Update cache
        StringBuilder autoDetectedLibrariesPref = new StringBuilder();
        for (Library lib : libraries) {
            autoDetectedLibrariesPref.append(DELIMITER).append(lib.getDefinedName());
        }
        sharedPreferences.edit().putInt("versionCode", pi.versionCode).putString("autoDetectedLibraries", autoDetectedLibrariesPref.toString()).apply();
    }
    return libraries;
}
Also used : SharedPreferences(android.content.SharedPreferences) PackageInfo(android.content.pm.PackageInfo) Library(com.mikepenz.aboutlibraries.entity.Library)

Example 3 with Library

use of com.mikepenz.aboutlibraries.entity.Library in project AboutLibraries by mikepenz.

the class Libs method modifyLibraries.

/**
 * @param modifications
 */
public void modifyLibraries(HashMap<String, HashMap<String, String>> modifications) {
    if (modifications != null) {
        for (Map.Entry<String, HashMap<String, String>> entry : modifications.entrySet()) {
            ArrayList<Library> foundLibs = findInExternalLibrary(entry.getKey(), true, 1);
            if (foundLibs == null || foundLibs.size() == 0) {
                foundLibs = findInInternalLibrary(entry.getKey(), true, 1);
            }
            if (foundLibs != null && foundLibs.size() == 1) {
                Library lib = foundLibs.get(0);
                for (Map.Entry<String, String> modification : entry.getValue().entrySet()) {
                    String key = modification.getKey().toUpperCase();
                    String value = modification.getValue();
                    if (key.equals(LibraryFields.AUTHOR_NAME.name())) {
                        lib.setAuthor(value);
                    } else if (key.equals(LibraryFields.AUTHOR_WEBSITE.name())) {
                        lib.setAuthorWebsite(value);
                    } else if (key.equals(LibraryFields.LIBRARY_NAME.name())) {
                        lib.setLibraryName(value);
                    } else if (key.equals(LibraryFields.LIBRARY_DESCRIPTION.name())) {
                        lib.setLibraryDescription(value);
                    } else if (key.equals(LibraryFields.LIBRARY_VERSION.name())) {
                        lib.setLibraryVersion(value);
                    } else if (key.equals(LibraryFields.LIBRARY_WEBSITE.name())) {
                        lib.setLibraryWebsite(value);
                    } else if (key.equals(LibraryFields.LIBRARY_OPEN_SOURCE.name())) {
                        lib.setOpenSource(Boolean.parseBoolean(value));
                    } else if (key.equals(LibraryFields.LIBRARY_REPOSITORY_LINK.name())) {
                        lib.setRepositoryLink(value);
                    } else if (key.equals(LibraryFields.LIBRARY_CLASSPATH.name())) {
                        // note this can be set but won't probably work for autodetect
                        lib.setClassPath(value);
                    } else if (key.equals(LibraryFields.LICENSE_NAME.name())) {
                        if (lib.getLicense() == null) {
                            lib.setLicense(new License());
                        }
                        lib.getLicense().setLicenseName(value);
                    } else if (key.equals(LibraryFields.LICENSE_SHORT_DESCRIPTION.name())) {
                        if (lib.getLicense() == null) {
                            lib.setLicense(new License());
                        }
                        lib.getLicense().setLicenseShortDescription(value);
                    } else if (key.equals(LibraryFields.LICENSE_DESCRIPTION.name())) {
                        if (lib.getLicense() == null) {
                            lib.setLicense(new License());
                        }
                        lib.getLicense().setLicenseDescription(value);
                    } else if (key.equals(LibraryFields.LICENSE_WEBSITE.name())) {
                        if (lib.getLicense() == null) {
                            lib.setLicense(new License());
                        }
                        lib.getLicense().setLicenseWebsite(value);
                    }
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) License(com.mikepenz.aboutlibraries.entity.License) Library(com.mikepenz.aboutlibraries.entity.Library) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Library

use of com.mikepenz.aboutlibraries.entity.Library in project AboutLibraries by mikepenz.

the class LibsBuilder method adapter.

/**
 * builder to build an adapter out of the given information ;D
 *
 * @param context the current context
 * @return a LibsRecyclerViewAdapter with the libraries
 */
public FastAdapter adapter(Context context) {
    Libs libs;
    if (fields == null) {
        libs = new Libs(context);
    } else {
        libs = new Libs(context, fields);
    }
    // apply modifications
    libs.modifyLibraries(libraryModification);
    // fetch the libraries and sort if a comparator was set
    ArrayList<Library> libraries = libs.prepareLibraries(context, internalLibraries, excludeLibraries, autoDetect, checkCachedDetection, sort);
    // prepare adapter
    ItemAdapter itemAdapter = new ItemAdapter();
    List<LibraryItem> libraryItems = new ArrayList<>();
    for (Library library : libraries) {
        libraryItems.add(new LibraryItem().withLibrary(library).withLibsBuilder(this));
    }
    FastAdapter fastAdapter = FastAdapter.with(itemAdapter);
    // noinspection unchecked
    itemAdapter.add(libraryItems);
    return fastAdapter;
}
Also used : LibraryItem(com.mikepenz.aboutlibraries.ui.item.LibraryItem) ArrayList(java.util.ArrayList) Library(com.mikepenz.aboutlibraries.entity.Library) FastAdapter(com.mikepenz.fastadapter.FastAdapter) ItemAdapter(com.mikepenz.fastadapter.adapters.ItemAdapter)

Example 5 with Library

use of com.mikepenz.aboutlibraries.entity.Library in project AboutLibraries by mikepenz.

the class Detect method detect.

public static List<Library> detect(Context mCtx, List<Library> libraries) {
    ArrayList<Library> foundLibraries = new ArrayList<>();
    // Loop through known libraries
    for (Library library : libraries) {
        if (!TextUtils.isEmpty(library.getClassPath())) {
            try {
                Context ctx = mCtx.createPackageContext(mCtx.getPackageName(), Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
                Class<?> clazz = Class.forName(library.getClassPath(), false, ctx.getClassLoader());
                // Detected a library!!!
                if (clazz != null) {
                    foundLibraries.add(library);
                }
            } catch (ClassNotFoundException e) {
            // e.printStackTrace();
            } catch (PackageManager.NameNotFoundException e) {
            // e.printStackTrace();
            }
        }
    }
    return foundLibraries;
}
Also used : Context(android.content.Context) PackageManager(android.content.pm.PackageManager) ArrayList(java.util.ArrayList) Library(com.mikepenz.aboutlibraries.entity.Library)

Aggregations

Library (com.mikepenz.aboutlibraries.entity.Library)11 ArrayList (java.util.ArrayList)6 License (com.mikepenz.aboutlibraries.entity.License)3 PackageInfo (android.content.pm.PackageInfo)2 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 PackageManager (android.content.pm.PackageManager)1 LibraryItem (com.mikepenz.aboutlibraries.ui.item.LibraryItem)1 FastAdapter (com.mikepenz.fastadapter.FastAdapter)1 ItemAdapter (com.mikepenz.fastadapter.adapters.ItemAdapter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1