Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,7 @@ public void onSelectItem(){
mShouldFinish = true;

Intent intent = new Intent(WPActionBarActivity.this, StatsActivity.class);
intent.putExtra("id", WordPress.getCurrentBlog().getLocalTableBlogId());
intent.putExtra("isNew", true);
intent.putExtra(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID, WordPress.getCurrentBlog().getLocalTableBlogId());
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityWithDelay(intent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,10 @@ private void showStatsActivity(int remoteBlogId) {
return;
}

// stats activity is designed to work with the current blog, so switch blogs if necessary
if (WordPress.getCurrentRemoteBlogId() != remoteBlogId) {
// TODO: should we show a toast to let user know blog was switched?
int localBlogId = WordPress.wpDB.getLocalTableBlogIdForRemoteBlogId(remoteBlogId);
WordPress.setCurrentBlog(localBlogId);
}

int localBlogId = WordPress.wpDB.getLocalTableBlogIdForRemoteBlogId(remoteBlogId);
Intent intent = new Intent(getActivity(), StatsActivity.class);
intent.putExtra(StatsActivity.ARG_NO_MENU_DRAWER, true);
intent.putExtra(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID, localBlogId);
getActivity().startActivity(intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void onCursorLoaded(final Uri uri, Cursor cursor) {

long date = cursor.getLong(colDate);

String timezone = StatsUtils.getBlogTimezone(WordPress.getCurrentBlog());
String timezone = StatsUtils.getBlogTimezone(WordPress.getBlog(getLocalTableBlogID()));
long currentDate = timezone != null ? StatsUtils.getCurrentDateMsTZ(timezone) : StatsUtils.getCurrentDateMs();

boolean isToday = timeframe.equals(StatsTimeframe.TODAY.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public abstract class StatsAbsViewFragment extends Fragment {
public static final String TAG = StatsAbsViewFragment.class.getSimpleName();

public static StatsAbsViewFragment newInstance(StatsViewType viewType) {
public static StatsAbsViewFragment newInstance(StatsViewType viewType, int localTableBlogID) {
StatsAbsViewFragment fragment = null;

switch (viewType) {
Expand Down Expand Up @@ -51,6 +51,7 @@ public static StatsAbsViewFragment newInstance(StatsViewType viewType) {

Bundle args = new Bundle();
args.putInt(ARGS_VIEW_TYPE, viewType.ordinal());
args.putInt(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID, localTableBlogID);
fragment.setArguments(args);

return fragment;
Expand All @@ -63,5 +64,9 @@ protected StatsViewType getViewType() {
return StatsViewType.values()[ordinal];
}

protected int getLocalTableBlogID() {
return getArguments().getInt(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID);
}

protected abstract String getTitle();
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.content.Loader;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
Expand Down Expand Up @@ -61,11 +60,12 @@ public class StatsBarGraphFragment extends Fragment implements LoaderManager.Loa
private int mLastHighlightedBar = -1;
private Tooltip mTooltip;

public static StatsBarGraphFragment newInstance(StatsBarChartUnit unit) {
public static StatsBarGraphFragment newInstance(StatsBarChartUnit unit, int localTableBlogID) {
StatsBarGraphFragment fragment = new StatsBarGraphFragment();

Bundle args = new Bundle();
args.putInt(ARGS_BAR_CHART_UNIT, unit.ordinal());
args.putInt(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID, localTableBlogID);
fragment.setArguments(args);

return fragment;
Expand Down Expand Up @@ -160,19 +160,9 @@ private void handleBarChartTap(int tappedBar) {
properties.put("unit", unit.name());
AnalyticsTracker.track(AnalyticsTracker.Stat.STATS_TAPPED_BAR_CHART, properties);
if (unit == StatsBarChartUnit.DAY) {
StatsUtils.StatsCredentials credentials = StatsUtils.getCurrentBlogStatsCredentials();
if (credentials == null) {
// Credentials empty, do nothing.
return;
}

String statsAuthenticatedUser = credentials.getUsername();
String statsAuthenticatedPassword = credentials.getPassword();
Intent statsWebViewIntent = new Intent(this.getActivity(), StatsDetailsActivity.class);
statsWebViewIntent.putExtra(StatsWebViewActivity.STATS_AUTHENTICATED_USER, statsAuthenticatedUser);
statsWebViewIntent.putExtra(StatsWebViewActivity.STATS_AUTHENTICATED_PASSWD,
statsAuthenticatedPassword);
statsWebViewIntent.putExtra(StatsActivity.STATS_DETAILS_DATE, date);
statsWebViewIntent.putExtra(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID, getLocalTableBlogID());
this.getActivity().startActivity(statsWebViewIntent);
} else {
// Week or Month on the screen. Show a toast.
Expand All @@ -184,19 +174,22 @@ private void handleBarChartTap(int tappedBar) {
}
}


private StatsBarChartUnit getBarChartUnit() {
int ordinal = getArguments().getInt(ARGS_BAR_CHART_UNIT);
return StatsBarChartUnit.values()[ordinal];
}

protected int getLocalTableBlogID() {
return getArguments().getInt(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (WordPress.getCurrentBlog() == null) {
if (WordPress.getBlog(getLocalTableBlogID()) == null) {
return null;
}

String blogId = WordPress.getCurrentBlog().getDotComBlogId();
String blogId = WordPress.getBlog(getLocalTableBlogID()).getDotComBlogId();
if (TextUtils.isEmpty(blogId)) {
blogId = "0";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected Fragment getFragment(int position) {

StatsCursorTreeFragment fragment = StatsCursorTreeFragment.newInstance(groupUri, childrenUri,
R.string.stats_entry_clicks_url, R.string.stats_totals_clicks, R.string.stats_empty_clicks_title,
R.string.stats_empty_clicks_desc);
R.string.stats_empty_clicks_desc, getLocalTableBlogID());
CustomAdapter adapter = new CustomAdapter(null, getActivity());
adapter.setCursorLoaderCallback(fragment);
fragment.setListAdapter(adapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public class StatsCommentsFragment extends StatsAbsPagedViewFragment {
protected Fragment getFragment(int position) {
if (position == 0) {
StatsCursorFragment fragment = StatsCursorFragment.newInstance(STATS_TOP_COMMENTERS_URI,
R.string.stats_entry_top_commenter, R.string.stats_totals_comments, R.string.stats_empty_comments);
R.string.stats_entry_top_commenter, R.string.stats_totals_comments, R.string.stats_empty_comments, getLocalTableBlogID());
fragment.setListAdapter(new CustomCursorAdapter(getActivity(), null, TOP_COMMENTERS));
fragment.setCallback(this);
return fragment;
} else if (position == 1) {
int entryLabelResId = R.string.stats_entry_most_commented;
int totalsLabelResId = R.string.stats_totals_comments;
StatsCursorFragment fragment = StatsCursorFragment.newInstance(STATS_MOST_COMMENTED_URI,
R.string.stats_entry_most_commented, R.string.stats_totals_comments, R.string.stats_empty_comments);
R.string.stats_entry_most_commented, R.string.stats_totals_comments, R.string.stats_empty_comments, getLocalTableBlogID());
fragment.setListAdapter(new CustomCursorAdapter(getActivity(), null, MOST_COMMENTED));
fragment.setCallback(this);
return fragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public class StatsCursorFragment extends Fragment implements LoaderManager.Loade
private StatsCursorInterface mCallback;

public static StatsCursorFragment newInstance(Uri uri, int entryLabelResId, int totalsLabelResId,
int emptyLabelTitleResId) {
return newInstance(uri, entryLabelResId, totalsLabelResId, emptyLabelTitleResId, NO_STRING_ID);
int emptyLabelTitleResId, int localTableBlogID) {
return newInstance(uri, entryLabelResId, totalsLabelResId, emptyLabelTitleResId, NO_STRING_ID, localTableBlogID);
}

public static StatsCursorFragment newInstance(Uri uri, int entryLabelResId, int totalsLabelResId,
int emptyLabelTitleResId, int emptyLabelDescResId) {
int emptyLabelTitleResId, int emptyLabelDescResId, int localTableBlogID) {
StatsCursorFragment fragment = new StatsCursorFragment();

Bundle args = new Bundle();
Expand All @@ -65,6 +65,7 @@ public static StatsCursorFragment newInstance(Uri uri, int entryLabelResId, int
args.putInt(ARGS_TOTALS_LABEL, totalsLabelResId);
args.putInt(ARGS_EMPTY_LABEL_TITLE, emptyLabelTitleResId);
args.putInt(ARGS_EMPTY_LABEL_DESC, emptyLabelDescResId);
args.putInt(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID, localTableBlogID);
fragment.setArguments(args);

return fragment;
Expand Down Expand Up @@ -119,6 +120,10 @@ private int getEmptyLabelDescResId() {
return getArguments().getInt(ARGS_EMPTY_LABEL_DESC);
}

private int getLocalTableBlogID() {
return getArguments().getInt(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Expand All @@ -127,10 +132,10 @@ public void onActivityCreated(Bundle savedInstanceState) {

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (WordPress.getCurrentBlog() == null)
if (WordPress.getBlog(getLocalTableBlogID()) == null)
return null;

String blogId = WordPress.getCurrentBlog().getDotComBlogId();
String blogId = WordPress.getBlog(getLocalTableBlogID()).getDotComBlogId();
if (TextUtils.isEmpty(blogId)) blogId = "0";
return new CursorLoader(getActivity(), getUri(), null, "blogId=?", new String[] { blogId }, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class StatsCursorTreeFragment extends Fragment

public static StatsCursorTreeFragment newInstance(Uri groupUri, Uri childrenUri, int entryLabelResId,
int totalsLabelResId, int emptyLabelTitleResId,
int emptyLabelDescResId) {
int emptyLabelDescResId, int localTableBlogID) {
StatsCursorTreeFragment fragment = new StatsCursorTreeFragment();

Bundle args = new Bundle();
Expand All @@ -71,6 +71,7 @@ public static StatsCursorTreeFragment newInstance(Uri groupUri, Uri childrenUri,
args.putInt(ARGS_TOTALS_LABEL, totalsLabelResId);
args.putInt(ARGS_EMPTY_LABEL_TITLE, emptyLabelTitleResId);
args.putInt(ARGS_EMPTY_LABEL_DESC, emptyLabelDescResId);
args.putInt(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID, localTableBlogID);
fragment.setArguments(args);

return fragment;
Expand Down Expand Up @@ -131,6 +132,10 @@ private int getEmptyLabelDescResId() {
return getArguments().getInt(ARGS_EMPTY_LABEL_DESC);
}

private int getLocalTableBlogID() {
return getArguments().getInt(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Expand All @@ -141,10 +146,10 @@ public void onActivityCreated(Bundle savedInstanceState) {

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (WordPress.getCurrentBlog() == null)
if (WordPress.getBlog(getLocalTableBlogID()) == null)
return null;

String blogId = WordPress.getCurrentBlog().getDotComBlogId();
String blogId = WordPress.getBlog(getLocalTableBlogID()).getDotComBlogId();
if (TextUtils.isEmpty(blogId))
blogId = "0";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class StatsDetailsActivity extends WPActionBarActivity {
private String mStatsDate = null;
private final Handler mHandler = new Handler();
private int mAltRowColor;
private int mLocalBlogID = -1;

// Variables that hold data returned from the REST API
private int mVisitorsCount = 0;
Expand Down Expand Up @@ -126,10 +127,13 @@ public void onRefreshStarted(View view) {
}
);

Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey(StatsActivity.STATS_DETAILS_DATE)) {
String date = extras.getString(StatsActivity.STATS_DETAILS_DATE);
mStatsDate = date;
if (getIntent() != null) {
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey(StatsActivity.STATS_DETAILS_DATE)) {
String date = extras.getString(StatsActivity.STATS_DETAILS_DATE);
mStatsDate = date;
}
mLocalBlogID = getIntent().getIntExtra(StatsActivity.ARG_LOCAL_TABLE_BLOG_ID, -1);
}

setTitle(getString(R.string.stats));
Expand Down Expand Up @@ -194,7 +198,7 @@ private void refreshStats() {
return;
}

final Blog currentBlog = WordPress.getCurrentBlog();
final Blog currentBlog = WordPress.getBlog(mLocalBlogID);

if (mStatsDate == null || currentBlog == null || !NetworkUtils.isNetworkAvailable(this)) {
mPullToRefreshHelper.setRefreshing(false);
Expand All @@ -204,7 +208,7 @@ private void refreshStats() {
return;
}

final String blogId = StatsUtils.getBlogId();
final String blogId = StatsUtils.getBlogId(mLocalBlogID);

// View and visitor counts for a site
final String viewAndVisitorsPath = String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected Fragment getFragment(int position) {

Uri uri = Uri.parse(STATS_GEOVIEWS_URI.toString() + "?timeframe=" + TIMEFRAMES[position].name());

StatsCursorFragment fragment = StatsCursorFragment.newInstance(uri, entryLabelResId, totalsLabelResId, emptyLabelResId);
StatsCursorFragment fragment = StatsCursorFragment.newInstance(uri, entryLabelResId, totalsLabelResId, emptyLabelResId, getLocalTableBlogID());
fragment.setListAdapter(new CustomCursorAdapter(getActivity(), null));
fragment.setCallback(this);
return fragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected Fragment getFragment(int position) {

StatsCursorTreeFragment fragment = StatsCursorTreeFragment.newInstance(groupUri, childrenUri,
R.string.stats_entry_referrers, R.string.stats_totals_views, R.string.stats_empty_referrers_title,
R.string.stats_empty_referrers_desc);
R.string.stats_empty_referrers_desc, getLocalTableBlogID());
CustomAdapter adapter = new CustomAdapter(null, getActivity());
adapter.setCursorLoaderCallback(fragment);
fragment.setListAdapter(adapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected Fragment getFragment(int position) {

StatsCursorFragment fragment = StatsCursorFragment.newInstance(uri, R.string.stats_entry_search_engine_terms,
R.string.stats_totals_views, R.string.stats_empty_search_engine_terms_title,
R.string.stats_empty_search_engine_terms_desc);
R.string.stats_empty_search_engine_terms_desc, getLocalTableBlogID());
fragment.setListAdapter(new CustomCursorAdapter(getActivity(), null));
fragment.setCallback(this);
return fragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
int entryLabelResId = R.string.stats_entry_tags_and_categories;
int totalsLabelResId = R.string.stats_totals_views;
int emptyLabelResId = R.string.stats_empty_tags_and_categories;
StatsCursorFragment fragment = StatsCursorFragment.newInstance(STATS_TAGS_AND_CATEGORIES_URI, entryLabelResId, totalsLabelResId, emptyLabelResId);
StatsCursorFragment fragment = StatsCursorFragment.newInstance(STATS_TAGS_AND_CATEGORIES_URI, entryLabelResId, totalsLabelResId, emptyLabelResId, getLocalTableBlogID());
fragment.setListAdapter(new CustomCursorAdapter(getActivity(), null));
fragment.setCallback(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected Fragment getFragment(int position) {

Uri uri = Uri.parse(STATS_TOP_AUTHORS_URI.toString() + "?timeframe=" + TIMEFRAMES[position].name());

StatsCursorFragment fragment = StatsCursorFragment.newInstance(uri, entryLabelResId, totalsLabelResId, emptyLabelResId);
StatsCursorFragment fragment = StatsCursorFragment.newInstance(uri, entryLabelResId, totalsLabelResId, emptyLabelResId, getLocalTableBlogID());
fragment.setListAdapter(new CustomCursorAdapter(getActivity(), null));
fragment.setCallback(this);
return fragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected Fragment getFragment(int position) {
Uri uri = Uri.parse(STATS_TOP_POSTS_AND_PAGES_URI.toString() + "?timeframe=" + TIMEFRAMES[position].name());

StatsCursorFragment fragment = StatsCursorFragment.newInstance(uri, R.string.stats_entry_posts_and_pages,
R.string.stats_totals_views, R.string.stats_empty_top_posts_title, R.string.stats_empty_top_posts_desc);
R.string.stats_totals_views, R.string.stats_empty_top_posts_title, R.string.stats_empty_top_posts_desc, getLocalTableBlogID());
fragment.setListAdapter(new CustomCursorAdapter(getActivity(), null));
fragment.setCallback(this);
return fragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,20 @@ public static int getSmallestWidthDP() {
}

/**
* Return the credentials for the current blog or null if not available.
* Return the credentials for the blog or null if not available.
*
* 1. Read the credentials at blog level (Jetpack connected with a wpcom account != main account)
* 2. If credentials are empty read the global wpcom credentials
* 3. Check that credentials are not empty before launching the activity
*
*/
public static StatsCredentials getCurrentBlogStatsCredentials() {
String statsAuthenticatedUser = WordPress.getCurrentBlog().getDotcom_username();
String statsAuthenticatedPassword = WordPress.getCurrentBlog().getDotcom_password();
public static StatsCredentials getBlogStatsCredentials(int localTableBlogID) {
Blog currentBlog = WordPress.getBlog(localTableBlogID);
if (currentBlog == null) {
return null;
}
String statsAuthenticatedUser = currentBlog.getDotcom_username();
String statsAuthenticatedPassword = currentBlog.getDotcom_password();

if (org.apache.commons.lang.StringUtils.isEmpty(statsAuthenticatedPassword)
|| org.apache.commons.lang.StringUtils.isEmpty(statsAuthenticatedUser)) {
Expand Down Expand Up @@ -343,8 +347,8 @@ public String getPassword() {
* </p>
* @return String blogId or null
*/
public static String getBlogId() {
Blog currentBlog = WordPress.getCurrentBlog();
public static String getBlogId(int localTableBlogID) {
Blog currentBlog = WordPress.getBlog(localTableBlogID);
if (currentBlog == null) {
return null;
}
Expand Down
Loading