Ejemplo n.º 1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = this;
    ctxt = getApplicationContext();
    mPrefs = getSharedPreferences("pref", 0);
    mGlobalPrefs = PreferenceManager.getDefaultSharedPreferences(ctxt);
    inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

    bar = new RelativeLayout(this);

    webLayout = (WebLayoutView) inflater.inflate(R.layout.page_web, null);
    browserListViewAdapter = new BrowserImageAdapter(this);
    webWindows = new Vector<CustomWebView>();

    // Initialize BookmarksManager
    if (BookmarksActivity.bookmarksMgr == null) {
      BookmarksManager loadedBookmarksMgr = BookmarksManager.loadBookmarksManager();
      if (loadedBookmarksMgr == null) {
        Log.d("LB", "BookmarksActivity.bookmarksMgr is null. Making new one");
        BookmarksActivity.bookmarksMgr = new BookmarksManager();
      } else {
        Log.d("LB", "BookmarksActivity.bookmarksMgr loaded");
        BookmarksActivity.bookmarksMgr = loadedBookmarksMgr;
      }
    } else {
      Log.d("LB", "BookmarksActivity.bookmarksMgr is not null");
    }

    // THIS IS TEMPORARY CODE TO TRANSFER FROM THE OLD BOOKMARKS SYSTEM TO THE NEW ONE
    int numBookmarksToTransfer = mPrefs.getInt("numbookmarkedpages", 0);
    Log.d("LB", numBookmarksToTransfer + " bookmarks need to transfer");
    for (int book = 0; book < numBookmarksToTransfer; book++) {
      String url = mPrefs.getString("bookmark" + book, null);
      String title = mPrefs.getString("bookmarktitle" + book, null);
      ;

      if (url != null) {
        Bookmark newBookmark = new Bookmark(url, title);
        try {
          URL curUrl = new URL(url);
          File imageFile = new File(getApplicationInfo().dataDir + "/icons/" + curUrl.getHost());
          if (imageFile.exists()) newBookmark.setPathToFavicon(imageFile.getAbsolutePath());
        } catch (Exception e) {
        }
        ;
        BookmarksActivity.bookmarksMgr.root.addBookmark(newBookmark);
      }
      mPrefs.edit().remove("bookmarktitle" + book).remove("bookmark" + book).commit();
      Log.d("LB", "bookmark" + book + " " + url + " transfered");
    }
    if (numBookmarksToTransfer != 0) {
      mPrefs.edit().remove("numbookmarkedpages").commit();
      BookmarksActivity.bookmarksMgr.saveBookmarksManager();
    }
    // ALL BOOKMARKS SHOULD NOW BE TRANSFERED TO THE NEW SYSTEM

    Point screenSize = new Point();
    screenSize.x = getWindow().getWindowManager().getDefaultDisplay().getWidth();
    screenSize.y = getWindow().getWindowManager().getDefaultDisplay().getHeight();

    if (Math.min(screenSize.x, screenSize.y) < 510) // px  //may need to be adjusted
    assetHomePage = "file:///android_asset/home_small.html";
    else assetHomePage = "file:///android_asset/home.html";

    Properties.update_preferences();

    if (Properties.sidebarProp.swapLayout)
      contentView = (DrawerLayout) inflater.inflate(R.layout.main_swapped, null);
    else contentView = (DrawerLayout) inflater.inflate(R.layout.main, null);

    contentFrame = ((FrameLayout) contentView.findViewById(R.id.content_frame));
    browserListView = (ListView) contentView.findViewById(R.id.right_drawer);
    toolbar = (Toolbar) contentView.findViewById(R.id.toolbar);

    contentFrame.addView(webLayout, 0);
    setSupportActionBar(toolbar);
    actionBar = getSupportActionBar();
    actionBarControls = new ActionBarControls(actionBar);
    setContentView(contentView);

    if (Properties.appProp.fullscreen)
      getWindow()
          .setFlags(
              WindowManager.LayoutParams.FLAG_FULLSCREEN,
              WindowManager.LayoutParams.FLAG_FULLSCREEN);

    int id = getResources().getIdentifier("config_enableTranslucentDecor", "bool", "android");

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
      if (Properties.appProp.transparentNav || Properties.appProp.TransparentStatus)
        if (id == 0) { // transparency is not supported
          Properties.appProp.transparentNav = false;
          Properties.appProp.TransparentStatus = false;
        }

    tintManager = new SystemBarTintManager(activity);

    //		if (Properties.appProp.transparentNav || Properties.appProp.TransparentStatus)
    //			if (id != 0) {
    // 		        if (Properties.appProp.transparentNav)
    //		        	getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
    // WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    //		        if (Properties.appProp.TransparentStatus)
    //		        	getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
    // WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    //		        if (Properties.appProp.fullscreen && Properties.appProp.transparentNav){
    //		        	if (Tools.hasSoftNavigation(activity))
    //		        		NavMargine= Tools.getNavBarHeight(getResources());
    //		        }else if (Properties.appProp.transparentNav){
    //		        	if (Tools.hasSoftNavigation(activity))
    //		        		NavMargine= Tools.getNavBarHeight(getResources());
    //		        }
    //
    //		        if (Properties.appProp.TransparentStatus){
    //		        	tintManager = new SystemBarTintManager(this);
    //		        	tintManager.setStatusBarTintEnabled(true);
    //		        	tintManager.setStatusBarTintColor(Properties.appProp.actionBarColor);
    //		        	if (Properties.appProp.fullscreen)
    //		        		tintManager.setTintAlpha(0.0f);
    //
    //		        }
    //		   }

    SetupLayouts.setuplayouts();

    Intent intent = getIntent();
    SharedPreferences savedInstancePreferences = getSharedPreferences("state", 0);
    int numSavedTabs = savedInstancePreferences.getInt("numtabs", 0);

    if (numSavedTabs > 0) {
      System.out.println("RESTORING STATE");
      String[] urls = new String[numSavedTabs];
      for (int I = 0; I < numSavedTabs; I++)
        urls[I] = savedInstancePreferences.getString("URL" + I, "http://www.google.com/");

      int tabNumber = savedInstancePreferences.getInt("tabNumber", 0);
      for (int I = 0; I < urls.length; I++) {
        webWindows.add(new CustomWebView(MainActivity.this, null, urls[I]));
        browserListViewAdapter.notifyDataSetChanged();
      }
      ((ViewGroup) webLayout.findViewById(R.id.webviewholder)).addView(webWindows.get(tabNumber));
      savedInstancePreferences.edit().clear().commit();
      savedInstancePreferences = null;
    } else { // If no InstanceState is found, just add a single page
      if (intent.getAction() != Intent.ACTION_WEB_SEARCH
          && intent.getAction()
              != Intent
                  .ACTION_VIEW) { // if page was requested from a different app, do not load home
                                  // page
        webWindows.add(new CustomWebView(MainActivity.this, null, null));
        ((ViewGroup) webLayout.findViewById(R.id.webviewholder)).addView(webWindows.get(0));
        browserListViewAdapter.notifyDataSetChanged();
      }
    }

    // detect if app was opened from a different app to open a site
    if (intent.getAction() == Intent.ACTION_WEB_SEARCH
        || intent.getAction() == Intent.ACTION_VIEW) {
      if (intent.getDataString() != null) {
        webWindows.add(new CustomWebView(MainActivity.this, null, intent.getDataString()));
        ((ViewGroup) webLayout.findViewById(R.id.webviewholder)).removeAllViews();
        ((ViewGroup) webLayout.findViewById(R.id.webviewholder))
            .addView(webWindows.get(webWindows.size() - 1));
        ((EditText) bar.findViewById(R.id.browser_searchbar)).setText(intent.getDataString());
        browserListViewAdapter.notifyDataSetChanged();
      }
    }

    if (Properties.appProp.systemPersistent) {
      Intent notificationIntent = new Intent(this, MainActivity.class);
      PendingIntent contentIntent =
          PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      NotificationCompat.Builder mBuilder =
          new NotificationCompat.Builder(this)
              .setSmallIcon(R.drawable.ic_stat_location_web_site)
              .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
              .setOngoing(true)
              .setContentIntent(contentIntent)
              .setPriority(2)
              .setContentTitle(getResources().getString(R.string.label));
      mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      mNotificationManager.notify(1, mBuilder.build());
    }

    View decorView = getWindow().getDecorView();

    decorView.setOnSystemUiVisibilityChangeListener(
        new View.OnSystemUiVisibilityChangeListener() {
          @Override
          public void onSystemUiVisibilityChange(int visibility) {
            if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
              if (tintManager != null && Properties.appProp.TransparentStatus)
                tintManager.setStatusBarTintEnabled(true);
              // Properties.appProp.fullscreen = true;
            } else {
              if (tintManager != null && Properties.appProp.TransparentStatus)
                tintManager.setStatusBarTintEnabled(false);
              // Properties.appProp.fullscreen = false;
            }
          }
        });
  }
Ejemplo n.º 2
0
  public void browserActionClicked(View v) {
    Handler handler = new Handler();
    Runnable r =
        new Runnable() {
          public void run() {
            contentView.closeDrawers();
          }
        };
    if (v.getId() != R.id.browser_bookmark) handler.postDelayed(r, 500);

    if (webWindows.size() == 0) {
      webWindows.add(new CustomWebView(MainActivity.this, null, null));
      ((ViewGroup) webLayout.findViewById(R.id.webviewholder)).removeAllViews();
      ((ViewGroup) webLayout.findViewById(R.id.webviewholder)).addView(webWindows.get(0));
    }

    Message msg = new Message();
    final CustomWebView WV = (CustomWebView) webLayout.findViewById(R.id.browser_page);
    switch (v.getId()) {
      case R.id.browser_home:
        WV.loadUrl(mPrefs.getString("browserhome", assetHomePage));
        WV.clearHistory();
        break;
      case R.id.browser_share:
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Link");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, WV.getUrl());
        startActivity(
            Intent.createChooser(sharingIntent, getResources().getString(R.string.share)));
        break;
      case R.id.browser_back:
        WV.goBack();
        break;
      case R.id.browser_forward:
        WV.goForward();
        break;
      case R.id.browser_refresh:
        if (WV.getProgress() != 100) WV.stopLoading();
        else WV.reload();
        break;
      case R.id.browser_bookmark:
        ImageButton BI = (ImageButton) MainActivity.bar.findViewById(R.id.browser_bookmark);
        // Find out if already a bookmark
        String url = WV.getUrl();
        if (WV != null && url != null) {
          String bookmarkName =
              BookmarksActivity.bookmarksMgr.root.containsBookmarkDeep(WV.getUrl());

          if (bookmarkName != null) {
            BookmarksActivity.bookmarksMgr.root.removeBookmarkDeep(bookmarkName);
            BI.setImageResource(R.drawable.btn_omnibox_bookmark_normal);
            System.out.println("BOOKMARK REMOVED!!");
          } else {
            Bookmark newBookmark = new Bookmark(url, WV.getTitle());

            try {
              Bitmap b = WV.getFavicon();
              if (b.getRowBytes() > 1) {
                new File(getApplicationInfo().dataDir + "/icons/").mkdirs();
                URL wvURL = new URL(url);
                String pathToFavicon = getApplicationInfo().dataDir + "/icons/" + wvURL.getHost();
                FileOutputStream out = new FileOutputStream(pathToFavicon);
                WV.getFavicon().compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
                Log.d("LB", "FAVICON SAVED AT " + pathToFavicon + "BITMAP IS " + b);
                newBookmark.setPathToFavicon(pathToFavicon);
              } else {

              }
            } catch (Exception e) {
              e.printStackTrace();
            }
            ;

            BookmarksActivity.bookmarksMgr.root.addBookmark(newBookmark);
            BI.setImageResource(R.drawable.btn_omnibox_bookmark_selected_normal);
            System.out.println("BOOKMARK SET!!");
          }

          BookmarksActivity.bookmarksMgr.saveBookmarksManager();
          CustomToolbar.colorizeToolbar(
              toolbar, Properties.appProp.primaryIntColor, MainActivity.activity);
        }
        break;
      case R.id.browser_find_on_page:
        actionBarControls.show();
        actionBarControls.lock(true);
        SetupLayouts.setUpFindBar();
        setUpFindBarListeners();
        suggestionsAdapter = null;
        // Focus on Find Bar
        TextView findText = (TextView) bar.findViewById(R.id.find_searchbar);
        findText.requestFocus();
        imm.showSoftInput(findText, InputMethodManager.SHOW_IMPLICIT);
        break;
      case R.id.browser_open_bookmarks:
        startActivity(new Intent(ctxt, BookmarksActivity.class));
        break;
      case R.id.browser_set_home:
        mPrefs.edit().putString("browserhome", WV.getUrl()).commit();
        msg.obj = WV.getTitle() + " set";
        msg.what = 1;
        messageHandler.sendMessage(msg);
        break;

      case R.id.browser_settings:
        if (mNotificationManager != null) mNotificationManager.cancel(1);
        startActivity(new Intent(ctxt, SettingsV2.class));
        onStop();
        android.os.Process.killProcess(android.os.Process.myPid());
        break;
      case R.id.browser_exit:
        doExiting();
        break;
      case R.id.browser_toggle_desktop:
        mGlobalPrefs
            .edit()
            .putBoolean("usedesktopview", !Properties.webpageProp.useDesktopView)
            .commit();
        Properties.webpageProp.useDesktopView = !Properties.webpageProp.useDesktopView;

        for (int I = 0; I < webWindows.size(); I++) {
          if (Properties.webpageProp.useDesktopView) {
            webWindows
                .get(I)
                .getSettings()
                .setUserAgentString(webWindows.get(I).createUserAgentString("desktop"));
            webWindows.get(I).getSettings().setLoadWithOverviewMode(true);
          } else {
            webWindows
                .get(I)
                .getSettings()
                .setUserAgentString(webWindows.get(I).createUserAgentString("mobile"));
            webWindows.get(I).getSettings().setLoadWithOverviewMode(false);
          }
          webWindows.get(I).reload();
        }
        break;
    }
  }