Android Open Source - StockTicker Stock Widget






From Project

Back to project page StockTicker.

License

The source code is released under:

MIT License

If you think the Android project StockTicker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.premnirmal.ticker.widget;
//from   w  w w . j  a v  a2 s.  c om
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.RemoteViews;

import com.github.premnirmal.tickerwidget.R;
import com.github.premnirmal.ticker.StocksApp;
import com.github.premnirmal.ticker.Tools;
import com.github.premnirmal.ticker.model.IStocksProvider;
import com.github.premnirmal.ticker.ui.ParanormalActivity;

import javax.inject.Inject;

/**
 * Created by premnirmal on 12/21/14.
 */
public class StockWidget extends AppWidgetProvider {

    public static final String ACTION_NAME = "OPEN_APP";

    @Inject
    IStocksProvider stocksProvider;

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (intent.getAction().equals(ACTION_NAME)) {
            context.startActivity(new Intent(context, ParanormalActivity.class));
        }
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        ((StocksApp) context.getApplicationContext()).inject(this);
        for (final Integer widgetId : appWidgetIds) {
            final Bundle options = appWidgetManager.getAppWidgetOptions(widgetId);
            final int min_width = getMinWidgetWidth(options);
            final RemoteViews remoteViews;
            if (min_width > 250) {
                remoteViews = new RemoteViews(context.getPackageName(),
                        R.layout.widget_4x1);
            } else {
                remoteViews = new RemoteViews(context.getPackageName(),
                        R.layout.widget_2x1);
            }
            updateWidget(context, appWidgetManager, widgetId, remoteViews);
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }

        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    private int getMinWidgetWidth(Bundle options) {
        if (options == null || !options.containsKey(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)) {
            return 0; // 2x1
        } else {
            return (int) options.get(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
        }
    }

    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        ((StocksApp) context.getApplicationContext()).inject(this);
        final int min_width = getMinWidgetWidth(newOptions);
        final RemoteViews remoteViews;
        if (min_width > 250) {
            remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget_4x1);
        } else {
            remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget_2x1);
        }
        updateWidget(context, appWidgetManager, appWidgetId, remoteViews);

        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }

    private void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, RemoteViews remoteViews) {
        remoteViews.setRemoteAdapter(R.id.list, new Intent(context, RemoteStockProviderService.class));
        final Intent startActivityIntent = new Intent(context, ParanormalActivity.class);
        final PendingIntent startActivityPendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setPendingIntentTemplate(R.id.list, startActivityPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.widget_layout, startActivityPendingIntent);
        remoteViews.setTextViewText(R.id.last_updated, "Last updated: " + stocksProvider.lastFetched());
        final float fontSize = Tools.getFontSize(context);
        remoteViews.setTextViewTextSize(R.id.last_updated, TypedValue.COMPLEX_UNIT_SP, fontSize);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.list);

        remoteViews.setInt(R.id.widget_layout, "setBackgroundColor", Tools.getBackgroundColor(context));
    }
}




Java Source Code List

com.github.premnirmal.ticker.AppModule.java
com.github.premnirmal.ticker.ApplicationTest.java
com.github.premnirmal.ticker.BaseActivity.java
com.github.premnirmal.ticker.StocksApp.java
com.github.premnirmal.ticker.Tools.java
com.github.premnirmal.ticker.UpdateReceiver.java
com.github.premnirmal.ticker.events.NoNetworkEvent.java
com.github.premnirmal.ticker.events.StockUpdatedEvent.java
com.github.premnirmal.ticker.model.HistoryProvider.java
com.github.premnirmal.ticker.model.IHistoryProvider.java
com.github.premnirmal.ticker.model.IStocksProvider.java
com.github.premnirmal.ticker.model.StocksProvider.java
com.github.premnirmal.ticker.model.StocksStorage.java
com.github.premnirmal.ticker.network.ApiModule.java
com.github.premnirmal.ticker.network.QueryCreator.java
com.github.premnirmal.ticker.network.QueryResults.java
com.github.premnirmal.ticker.network.Query.java
com.github.premnirmal.ticker.network.Results.java
com.github.premnirmal.ticker.network.StockQuery.java
com.github.premnirmal.ticker.network.Stock.java
com.github.premnirmal.ticker.network.StocksApi.java
com.github.premnirmal.ticker.network.StupidYahooWrapConverter.java
com.github.premnirmal.ticker.network.SuggestionApi.java
com.github.premnirmal.ticker.network.Suggestion.java
com.github.premnirmal.ticker.network.Suggestions.java
com.github.premnirmal.ticker.network.historicaldata.HistoricalData.java
com.github.premnirmal.ticker.network.historicaldata.History.java
com.github.premnirmal.ticker.network.historicaldata.Query.java
com.github.premnirmal.ticker.network.historicaldata.Quote.java
com.github.premnirmal.ticker.settings.FileExportTask.java
com.github.premnirmal.ticker.settings.FileImportTask.java
com.github.premnirmal.ticker.settings.SettingsActivity.java
com.github.premnirmal.ticker.ui.GraphActivity.java
com.github.premnirmal.ticker.ui.ParanormalActivity.java
com.github.premnirmal.ticker.ui.StocksAdapter.java
com.github.premnirmal.ticker.ui.SuggestionsAdapter.java
com.github.premnirmal.ticker.ui.TickerSelectorActivity.java
com.github.premnirmal.ticker.widget.RemoteStockProviderService.java
com.github.premnirmal.ticker.widget.RemoteStockViewAdapter.java
com.github.premnirmal.ticker.widget.StockWidget.java
com.terlici.dragndroplist.DragNDropAdapter.java
com.terlici.dragndroplist.DragNDropCursorAdapter.java
com.terlici.dragndroplist.DragNDropListView.java
com.terlici.dragndroplist.DragNDropSimpleAdapter.java