Bug 766389 - (3 of 3) Add apps promo box to about:home. r=sriram
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 22 Aug 2012 19:52:54 -0400
changeset 105116 daa163c8e26b4a9745fd005e9aaef7ddfc41cd80
parent 105115 98071de42c1c7be4f4c17495c98c1d192781be9e
child 105117 7f20dca58350d92becc25f5e63b62074d88dc45a
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewerssriram
bugs766389
milestone17.0a1
Bug 766389 - (3 of 3) Add apps promo box to about:home. r=sriram
mobile/android/base/AboutHomeContent.java
mobile/android/base/AboutHomePromoBox.java
--- a/mobile/android/base/AboutHomeContent.java
+++ b/mobile/android/base/AboutHomeContent.java
@@ -47,16 +47,17 @@ import android.widget.TextView;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.EnumSet;
 import java.util.List;
+import java.util.Random;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
 public class AboutHomeContent extends ScrollView
        implements TabsAccessor.OnQueryTabsCompleteListener {
     private static final String LOGTAG = "GeckoAboutHome";
 
     private static final int NUMBER_OF_REMOTE_TABS = 5;
@@ -81,16 +82,17 @@ public class AboutHomeContent extends Sc
 
     private AccountManager mAccountManager;
     private OnAccountsUpdateListener mAccountListener = null;
 
     protected SimpleCursorAdapter mTopSitesAdapter;
     protected GridView mTopSitesGrid;
 
     private AboutHomePromoBox mPromoBox;
+    private AboutHomePromoBox.Type mPrelimPromoBoxType;
     protected AboutHomeSection mAddons;
     protected AboutHomeSection mLastTabs;
     protected AboutHomeSection mRemoteTabs;
 
     private View.OnClickListener mRemoteTabClickListener;
     private OnInterceptTouchListener mOnInterceptTouchListener;
 
     public interface UriLoadCallback {
@@ -132,16 +134,19 @@ public class AboutHomeContent extends Sc
                 } catch (Exception e) {
                     Log.e(LOGTAG, "error building JSON arguments");
                 }
     
                 Log.d(LOGTAG, "Sending message to Gecko: " + SystemClock.uptimeMillis() + " - Tab:Add");
                 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Add", args.toString()));
             }
         };
+
+        mPrelimPromoBoxType = (new Random()).nextFloat() < 0.5 ? AboutHomePromoBox.Type.SYNC :
+                AboutHomePromoBox.Type.APPS;
     }
 
     private void inflate() {
         mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         mInflater.inflate(R.layout.abouthome_content, this);
 
         mTopSitesGrid = (GridView)findViewById(R.id.top_sites_grid);
         mTopSitesGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@@ -215,17 +220,20 @@ public class AboutHomeContent extends Sc
         else
             mPromoBox.hide();
     }
 
     private void updateLayout(boolean syncIsSetup) {
         boolean hasTopSites = mTopSitesAdapter.getCount() > 0;
 
         setTopSitesVisibility(hasTopSites);
-        setPromoBoxVisibility(!syncIsSetup, AboutHomePromoBox.Type.SYNC);
+        if (!syncIsSetup && mPrelimPromoBoxType == AboutHomePromoBox.Type.SYNC)
+            setPromoBoxVisibility(true, AboutHomePromoBox.Type.SYNC);
+        else
+            setPromoBoxVisibility(true, AboutHomePromoBox.Type.APPS);
     }
 
     private void updateLayoutForSync() {
         final GeckoApp.StartupMode startupMode = mActivity.getStartupMode();
         final boolean syncIsSetup = SyncAccounts.syncAccountsExist(mContext);
 
         post(new Runnable() {
             public void run() {
--- a/mobile/android/base/AboutHomePromoBox.java
+++ b/mobile/android/base/AboutHomePromoBox.java
@@ -1,18 +1,21 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.sync.setup.activities.SetupSyncActivity;
 
+import org.json.JSONObject;
+
 import android.content.Context;
 import android.content.Intent;
+import android.os.SystemClock;
 import android.text.SpannableString;
 import android.text.style.StyleSpan;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
@@ -22,17 +25,17 @@ import android.widget.TextView;
  * A promotional box for the about:home page. The layout contains an ImageView to the left of a
  * TextView whose resources may be overidden to display custom values for a new type of promo box.
  * To do this, add a new Type value and update show() to call setResources() for your values -
  * including a set[Box Type]Resources() helper method is recommended.
  */
 public class AboutHomePromoBox extends LinearLayout implements View.OnClickListener {
     private static final String LOGTAG = "AboutHomePromoBox";
 
-    public enum Type { SYNC };
+    public enum Type { SYNC, APPS };
 
     private Type mType;
 
     private final Context mContext;
     private final TextView mTextView;
     private final ImageView mImageView;
 
     // Use setResources() to set these variables for each PromoBox type.
@@ -56,16 +59,30 @@ public class AboutHomePromoBox extends L
     public void onClick(View v) {
         switch (mType) {
             case SYNC:
                 final Context context = v.getContext();
                 final Intent intent = new Intent(context, SetupSyncActivity.class);
                 context.startActivity(intent);
                 break;
 
+            case APPS:
+                final String url = "https://marketplace.mozilla.org";
+                final JSONObject args = new JSONObject();
+                try {
+                    args.put("url", url);
+                    args.put("engine", null);
+                    args.put("userEntered", false);
+                } catch (Exception e) {
+                    Log.e(LOGTAG, "error building JSON arguments");
+                }
+                Log.d(LOGTAG, "Sending message to Gecko: " + SystemClock.uptimeMillis() + " - Tab:Add");
+                GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Add", args.toString()));
+                break;
+
             default:
                 Log.e(LOGTAG, "Invalid type was set when promo box was clicked.");
                 break;
         }
     }
 
     /**
      * Shows the specified promo box. If a promo box is already active, it will be overidden with a
@@ -73,18 +90,22 @@ public class AboutHomePromoBox extends L
      */
     public void show(Type type) {
         mType = type;
         switch (type) {
             case SYNC:
                 setSyncResources();
                 break;
 
+            case APPS:
+                setAppsResources();
+                break;
+
             default:
-                Log.e(LOGTAG, "Invalid PromoBoxType specified.");
+                Log.e(LOGTAG, "show() - Invalid AboutHomePromoBox.Type specified.");
                 break;
         }
         updateViewResources();
         setVisibility(View.VISIBLE);
     }
 
     public void hide() {
         setVisibility(View.GONE);
@@ -116,9 +137,15 @@ public class AboutHomePromoBox extends L
         }
     }
 
     // Type.SYNC: Setup Firefox sync.
     private void setSyncResources() {
         setResources(R.string.abouthome_about_sync, R.string.abouthome_sync_bold_name,
                 R.drawable.abouthome_promo_logo_sync);
     }
+
+    // Types.APPS: Visit the Marketplace.
+    private void setAppsResources() {
+        setResources(R.string.abouthome_about_apps, R.string.abouthome_apps_bold_name,
+                R.drawable.abouthome_promo_logo_apps);
+    }
 }