2012-08-19 21:10:00 -07:00
|
|
|
/* 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 android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.style.StyleSpan;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
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.
|
|
|
|
*/
|
2012-12-20 16:49:51 -08:00
|
|
|
public class AboutHomePromoBox extends TextView implements View.OnClickListener {
|
2012-08-19 21:10:00 -07:00
|
|
|
private static final String LOGTAG = "AboutHomePromoBox";
|
|
|
|
|
2012-09-26 05:24:27 -07:00
|
|
|
public enum Type { NONE, SYNC, APPS };
|
2012-08-19 21:10:00 -07:00
|
|
|
|
|
|
|
private Type mType;
|
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
|
|
|
|
// Use setResources() to set these variables for each PromoBox type.
|
|
|
|
private int mTextResource;
|
|
|
|
private int mBoldTextResource;
|
|
|
|
private int mImageResource;
|
|
|
|
|
|
|
|
public AboutHomePromoBox(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
|
|
|
|
mContext = context;
|
|
|
|
setOnClickListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
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;
|
|
|
|
|
2012-08-22 16:52:54 -07:00
|
|
|
case APPS:
|
2012-10-15 12:51:38 -07:00
|
|
|
Tabs.getInstance().loadUrl("https://marketplace.mozilla.org", Tabs.LOADURL_NEW_TAB);
|
2012-08-22 16:52:54 -07:00
|
|
|
break;
|
|
|
|
|
2012-08-19 21:10:00 -07:00
|
|
|
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
|
|
|
|
* promo box of the specified type.
|
|
|
|
*/
|
|
|
|
public void show(Type type) {
|
|
|
|
mType = type;
|
|
|
|
switch (type) {
|
|
|
|
case SYNC:
|
|
|
|
setSyncResources();
|
|
|
|
break;
|
|
|
|
|
2012-08-22 16:52:54 -07:00
|
|
|
case APPS:
|
|
|
|
setAppsResources();
|
|
|
|
break;
|
|
|
|
|
2012-08-19 21:10:00 -07:00
|
|
|
default:
|
2012-08-22 16:52:54 -07:00
|
|
|
Log.e(LOGTAG, "show() - Invalid AboutHomePromoBox.Type specified.");
|
2012-09-26 05:24:27 -07:00
|
|
|
hide();
|
|
|
|
return;
|
2012-08-19 21:10:00 -07:00
|
|
|
}
|
|
|
|
updateViewResources();
|
|
|
|
setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void hide() {
|
|
|
|
setVisibility(View.GONE);
|
|
|
|
mType = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setResources(int textResource, int boldTextResource, int imageResource) {
|
|
|
|
mTextResource = textResource;
|
|
|
|
mBoldTextResource = boldTextResource;
|
|
|
|
mImageResource = imageResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateViewResources() {
|
|
|
|
updateTextViewResources();
|
2012-12-20 16:49:51 -08:00
|
|
|
setCompoundDrawablesWithIntrinsicBounds(mImageResource, 0, 0, 0);
|
2012-08-19 21:10:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updateTextViewResources() {
|
2012-08-22 16:52:54 -07:00
|
|
|
final String promoText = mContext.getResources().getString(mTextResource);
|
2012-08-19 21:10:00 -07:00
|
|
|
final String boldName = mContext.getResources().getString(mBoldTextResource);
|
|
|
|
final int styleIndex = promoText.indexOf(boldName);
|
|
|
|
if (styleIndex < 0)
|
2012-12-20 16:49:51 -08:00
|
|
|
setText(promoText);
|
2012-08-19 21:10:00 -07:00
|
|
|
else {
|
|
|
|
final SpannableString spannableText = new SpannableString(promoText);
|
|
|
|
spannableText.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), styleIndex,
|
|
|
|
styleIndex + boldName.length(), 0);
|
2012-12-20 16:49:51 -08:00
|
|
|
setText(spannableText, TextView.BufferType.SPANNABLE);
|
2012-08-19 21:10:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type.SYNC: Setup Firefox sync.
|
|
|
|
private void setSyncResources() {
|
|
|
|
setResources(R.string.abouthome_about_sync, R.string.abouthome_sync_bold_name,
|
2012-08-22 16:52:54 -07:00
|
|
|
R.drawable.abouthome_promo_logo_sync);
|
2012-08-19 21:10:00 -07:00
|
|
|
}
|
2012-08-22 16:52:54 -07:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2012-08-19 21:10:00 -07:00
|
|
|
}
|