public static VendorCompat getVendorCompat(Context context) { if (cachedCompat != null) return cachedCompat; // Application-wide configuration Context appContext = context.getApplicationContext(); // PlayHaven specific configuration for this Application SharedPreferences pref = appContext.getSharedPreferences(SHARED_PREF_NAME, SHARED_PREF_MODE); String pluginType = pref.getString(Config.PluginType.toString(), VendorCompat.class.getCanonicalName()); String pluginId = pref.getString(Config.PluginIdentifer.toString(), "android"); if (pluginId == null || pluginType == null) { d("getVendorCompat: using default"); return createDefaultVendorCompat(context); } VendorCompat compat = null; Class cls = null; // Class to load try { cls = Class.forName(pluginType); } catch (ClassNotFoundException e) { d(e, "getVendorCompat: failed to find: %s/%s", pluginType, pluginId); compat = createDefaultVendorCompat(context); } // Attempt two-argument constructor if (compat == null) { try { @SuppressWarnings("unchecked") Constructor con = cls.getConstructor(Context.class, String.class); compat = (VendorCompat) con.newInstance(context, pluginId); d("getVendorCompat: instantiated #1: %s/%s", pluginType, pluginId); } catch (Exception e) { d(e, "getVendorCompat: failed to instantiate #1: %s/%s", pluginType, pluginId); } } // Attempt one-argument constructor if (compat == null) { try { @SuppressWarnings("unchecked") Constructor con = cls.getConstructor(String.class); compat = (VendorCompat) con.newInstance(pluginId); d("getVendorCompat: instantiated #2: %s/%s", pluginType, pluginId); } catch (Exception e) { d(e, "getVendorCompat: failed to instantiate #2: %s/%s", pluginType, pluginId); } } if (compat == null) compat = createDefaultVendorCompat(context); setVendorCompat(context, compat); return compat; }
public static void setVendorCompat(Context context, VendorCompat compat) { cachedCompat = compat; // Application-wide configuration Context appContext = context.getApplicationContext(); // PlayHaven specific configuration for this Application SharedPreferences pref = appContext.getSharedPreferences(SHARED_PREF_NAME, SHARED_PREF_MODE); SharedPreferences.Editor editor = pref.edit(); // Set the property editor.putString(Config.PluginIdentifer.toString(), compat.getVendorId()); editor.putString(Config.PluginType.toString(), compat.getClass().getCanonicalName()); // And commit it editor.commit(); i("PlayHaven plugin identifier set: %s", compat.getVendorId()); debugConfig(context); }