private void loadBlockedDomainsList(@NonNull final Context context) { BrowserApp.getTaskThread() .execute( new Runnable() { @Override public void run() { AssetManager asset = context.getAssets(); BufferedReader reader = null; try { //noinspection IOResourceOpenedButNotSafelyClosed reader = new BufferedReader( new InputStreamReader(asset.open(BLOCKED_DOMAINS_LIST_FILE_NAME))); String line; while ((line = reader.readLine()) != null) { mBlockedDomainsList.add(line.trim()); } } catch (IOException e) { Log.wtf( TAG, "Reading blocked domains list from file '" + BLOCKED_DOMAINS_LIST_FILE_NAME + "' failed.", e); } finally { Utils.close(reader); } } }); }
@Inject public AdBlock(@NonNull Context context) { BrowserApp.getAppComponent().inject(this); if (mBlockedDomainsList.isEmpty() && Constants.FULL_VERSION) { loadHostsFile(context); } mBlockAds = mPreferenceManager.getAdBlockEnabled(); }
/** * This method reads through a hosts file and extracts the domains that should be redirected to * localhost (a.k.a. IP address 127.0.0.1). It can handle files that simply have a list of * hostnames to block, or it can handle a full blown hosts file. It will strip out comments, * references to the base IP address and just extract the domains to be used * * @param context the context needed to read the file */ private void loadHostsFile(@NonNull final Context context) { BrowserApp.getTaskThread() .execute( new Runnable() { @Override public void run() { AssetManager asset = context.getAssets(); BufferedReader reader = null; try { //noinspection IOResourceOpenedButNotSafelyClosed reader = new BufferedReader( new InputStreamReader(asset.open(BLOCKED_DOMAINS_LIST_FILE_NAME))); String line; while ((line = reader.readLine()) != null) { if (!line.isEmpty() && !line.startsWith(COMMENT)) { line = line.replace(LOCAL_IP_V4, EMPTY) .replace(LOCAL_IP_V4_ALT, EMPTY) .replace(LOCAL_IP_V6, EMPTY) .replace(TAB, EMPTY); int comment = line.indexOf(COMMENT); if (comment >= 0) { line = line.substring(0, comment); } line = line.trim(); if (!line.isEmpty() && !line.equals(LOCALHOST)) { while (line.contains(SPACE)) { int space = line.indexOf(SPACE); String host = line.substring(0, space); mBlockedDomainsList.add(host.trim()); line = line.substring(space, line.length()).trim(); } mBlockedDomainsList.add(line.trim()); } } } } catch (IOException e) { Log.wtf( TAG, "Reading blocked domains list from file '" + BLOCKED_DOMAINS_LIST_FILE_NAME + "' failed.", e); } finally { Utils.close(reader); } } }); }
public TabsFragment() { BrowserApp.getAppComponent().inject(this); }