<commit_before>package acr.browser.lightning.activity;

import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;

import com.squareup.otto.Bus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.dialog.BrowserDialog;
import acr.browser.lightning.preference.PreferenceManager;

import com.anthonycr.bonsai.Action;
import com.anthonycr.bonsai.Observable;
import com.anthonycr.bonsai.OnSubscribe;
import com.anthonycr.bonsai.Schedulers;
import com.anthonycr.bonsai.Subscriber;

import acr.browser.lightning.utils.FileUtils;
import acr.browser.lightning.utils.UrlUtils;
import acr.browser.lightning.view.LightningView;

/**
 * A manager singleton that holds all the {@link LightningView}
 * and tracks the current tab. It handles creation, deletion,
 * restoration, state saving, and switching of tabs.
 */
public class TabsManager {

    private static final String TAG = TabsManager.class.getSimpleName();
    private static final String BUNDLE_KEY = "WEBVIEW_";
    private static final String URL_KEY = "URL_KEY";
    private static final String BUNDLE_STORAGE = "SAVED_TABS.parcel";

    private final List<LightningView> mTabList = new ArrayList<>(1);
    @Nullable private LightningView mCurrentTab;
    @Nullable private TabNumberChangedListener mTabNumberListener;

    private boolean mIsInitialized = false;
    private final List<Runnable> mPostInitializationWorkList = new ArrayList<>();

    @Inject PreferenceManager mPreferenceManager;
    @Inject BookmarkManager mBookmarkManager;
    @Inject HistoryDatabase mHistoryManager;
    @Inject Bus mEventBus;
    @Inject Application mApp;

    public TabsManager() {
        BrowserApp.getAppComponent().inject(this);
    }

    // TODO remove and make presenter call new tab methods so it always knows
    @Deprecated
    public interface TabNumberChangedListener {
        void tabNumberChanged(int newNumber);
    }

    public void setTabNumberChangedListener(@Nullable TabNumberChangedListener listener) {
        mTabNumberListener = listener;
    }

    public void cancelPendingWork() {
        mPostInitializationWorkList.clear();
    }

    public synchronized void doAfterInitialization(@NonNull Runnable runnable) {
        if (mIsInitialized) {
            runnable.run();
        } else {
            mPostInitializationWorkList.add(runnable);
        }
    }

    private synchronized void finishInitialization() {
        mIsInitialized = true;
        for (Runnable runnable : mPostInitializationWorkList) {
            runnable.run();
        }
    }

    /**
     * Restores old tabs that were open before the browser
     * was closed. Handles the intent used to open the browser.
     *
     * @param activity  the activity needed to create tabs.
     * @param intent    the intent that started the browser activity.
     * @param incognito whether or not we are in incognito mode.
     */
    public synchronized Observable<Void> initializeTabs(@NonNull final Activity activity,
                                                        @Nullable final Intent intent,
                                                        final boolean incognito) {
        return Observable.create(new Action<Void>() {
            @Override
            public void onSubscribe(@NonNull final Subscriber<Void> subscriber) {

                // Make sure we start with a clean tab list
                shutdown();

                String url = null;
                if (intent != null) {
                    url = intent.getDataString();
                }

                // If incognito, only create one tab
                if (incognito) {
                    newTab(activity, url, true);
                    subscriber.onComplete();
                    return;
                }

                Log.d(TAG, "URL from intent: " + url);
                mCurrentTab = null;
                if (mPreferenceManager.getRestoreLostTabsEnabled()) {
                    restoreLostTabs(url, activity, subscriber);
                } else {
                    newTab(activity, null, false);
                    finishInitialization();
                    subscriber.onComplete();
                }

            }
        });

    }<commit_msg>Fixed bug where links wouldn't open correctly in the browser<commit_after>package acr.browser.lightning.activity;

import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;

import com.squareup.otto.Bus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.dialog.BrowserDialog;
import acr.browser.lightning.preference.PreferenceManager;

import com.anthonycr.bonsai.Action;
import com.anthonycr.bonsai.Observable;
import com.anthonycr.bonsai.OnSubscribe;
import com.anthonycr.bonsai.Schedulers;
import com.anthonycr.bonsai.Subscriber;

import acr.browser.lightning.utils.FileUtils;
import acr.browser.lightning.utils.UrlUtils;
import acr.browser.lightning.view.LightningView;

/**
 * A manager singleton that holds all the {@link LightningView}
 * and tracks the current tab. It handles creation, deletion,
 * restoration, state saving, and switching of tabs.
 */
public class TabsManager {

    private static final String TAG = TabsManager.class.getSimpleName();
    private static final String BUNDLE_KEY = "WEBVIEW_";
    private static final String URL_KEY = "URL_KEY";
    private static final String BUNDLE_STORAGE = "SAVED_TABS.parcel";

    private final List<LightningView> mTabList = new ArrayList<>(1);
    @Nullable private LightningView mCurrentTab;
    @Nullable private TabNumberChangedListener mTabNumberListener;

    private boolean mIsInitialized = false;
    private final List<Runnable> mPostInitializationWorkList = new ArrayList<>();

    @Inject PreferenceManager mPreferenceManager;
    @Inject BookmarkManager mBookmarkManager;
    @Inject HistoryDatabase mHistoryManager;
    @Inject Bus mEventBus;
    @Inject Application mApp;

    public TabsManager() {
        BrowserApp.getAppComponent().inject(this);
    }

    // TODO remove and make presenter call new tab methods so it always knows
    @Deprecated
    public interface TabNumberChangedListener {
        void tabNumberChanged(int newNumber);
    }

    public void setTabNumberChangedListener(@Nullable TabNumberChangedListener listener) {
        mTabNumberListener = listener;
    }

    public void cancelPendingWork() {
        mPostInitializationWorkList.clear();
    }

    public synchronized void doAfterInitialization(@NonNull Runnable runnable) {
        if (mIsInitialized) {
            runnable.run();
        } else {
            mPostInitializationWorkList.add(runnable);
        }
    }

    private synchronized void finishInitialization() {
        mIsInitialized = true;
        for (Runnable runnable : mPostInitializationWorkList) {
            runnable.run();
        }
    }

    /**
     * Restores old tabs that were open before the browser
     * was closed. Handles the intent used to open the browser.
     *
     * @param activity  the activity needed to create tabs.
     * @param intent    the intent that started the browser activity.
     * @param incognito whether or not we are in incognito mode.
     */
    public synchronized Observable<Void> initializeTabs(@NonNull final Activity activity,
                                                        @Nullable final Intent intent,
                                                        final boolean incognito) {
        return Observable.create(new Action<Void>() {
            @Override
            public void onSubscribe(@NonNull final Subscriber<Void> subscriber) {

                // Make sure we start with a clean tab list
                shutdown();

                String url = null;
                if (intent != null) {
                    url = intent.getDataString();
                }

                // If incognito, only create one tab
                if (incognito) {
                    newTab(activity, url, true);
                    subscriber.onComplete();
                    return;
                }

                Log.d(TAG, "URL from intent: " + url);
                mCurrentTab = null;
                if (mPreferenceManager.getRestoreLostTabsEnabled()) {
                    restoreLostTabs(url, activity, subscriber);
                } else {
                    if (!TextUtils.isEmpty(url)) {
                        newTab(activity, url, false);
                    } else {
                        newTab(activity, null, false);
                    }
                    finishInitialization();
                    subscriber.onComplete();
                }

            }
        });

    }
{'fraction_non_alphanumeric': 0.08017165707597776, 'fraction_numerical': 0.00039012971813127864, 'mean_word_length': 2.5690915419422207, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3139809', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2616, 'n_words': 691}