Android Open Source - StockTicker Tools






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;
//from   w  w  w  . ja v  a 2s .  c o m
import android.app.AlarmManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.SystemClock;

import com.github.premnirmal.tickerwidget.R;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.DateTimeZone;
import org.joda.time.MutableDateTime;

import java.io.File;
import java.util.List;

/**
 * Created by premnirmal on 12/22/14.
 */
public final class Tools {

    public static final String PREFS_NAME = "com.github.premnirmal.ticker";
    public static final String FONT_SIZE = "com.github.premnirmal.ticker.textsize";
    public static final String SETTING_AUTOSORT = "SETTING_AUTOSORT";
    public static final String WIDGET_BG = "WIDGET_BG";
    public static final String UPDATE_INTERVAL = "UPDATE_INTERVAL";
    public static final int TRANSPARENT = 0;
    public static final int TRANSLUCENT = 1;

    public static int getBackgroundColor(Context context) {
        return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .getInt(WIDGET_BG, TRANSPARENT) == TRANSPARENT ? TRANSPARENT
                : context.getResources().getColor(R.color.translucent);
    }

    public static float getFontSize(Context context) {
        final int size = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .getInt(FONT_SIZE, 1);
        switch (size) {
            case 0:
                return context.getResources().getInteger(R.integer.text_size_small);
            case 2:
                return context.getResources().getInteger(R.integer.text_size_large);
            case 1:
            default:
                return context.getResources().getInteger(R.integer.text_size_medium);
        }
    }

    public static File getTickersFile() {
        final File dir = Environment.getExternalStoragePublicDirectory("StockTickers");
        if (!dir.exists()) {
            dir.mkdir();
        }
        final String fileName = "Tickers.txt";
        final File file = new File(dir, fileName);
        return file;
    }

    public static boolean isNetworkOnline(Context context) {
        try {
            final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo i = connectivityManager.getActiveNetworkInfo();
            if (i == null)
                return false;
            if (!i.isConnected())
                return false;
            if (!i.isAvailable())
                return false;
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * Takes care of weekends and afterhours
     *
     * @return
     */
    public static long getMsToNextAlarm(Context context) {
        final SharedPreferences preferences = context.getSharedPreferences(Tools.PREFS_NAME, Context.MODE_PRIVATE);

        final int hourOfDay = DateTime.now().hourOfDay().get();
        final int dayOfWeek = DateTime.now().getDayOfWeek();
        final MutableDateTime mutableDateTime = new MutableDateTime(DateTime.now());
        mutableDateTime.setZone(DateTimeZone.getDefault());

        boolean set = false;

        if (hourOfDay >= 17) { // 5pm
            mutableDateTime.addDays(1);
            mutableDateTime.setHourOfDay(9); // 9am
            mutableDateTime.setMinuteOfHour(15); // update at 9:15am
            set = true;
        }

        if (set && dayOfWeek == DateTimeConstants.FRIDAY) {
            mutableDateTime.addDays(2);
        }

        if (dayOfWeek > DateTimeConstants.FRIDAY) {
            if (dayOfWeek == DateTimeConstants.SATURDAY) {
                mutableDateTime.addDays(set ? 1 : 2);
            } else if (dayOfWeek == DateTimeConstants.SUNDAY) {
                if(!set) {
                    mutableDateTime.addDays(1);
                }
            }
            set = true;
            mutableDateTime.setHourOfDay(9); // 9am
            mutableDateTime.setMinuteOfHour(15); // update at 9:15am
        }
        final int updatePref = preferences.getInt(UPDATE_INTERVAL,1);
        final long time = AlarmManager.INTERVAL_FIFTEEN_MINUTES * (updatePref + 1);
        if (set) {
            final long msToNextSchedule = mutableDateTime.getMillis() - DateTime.now().getMillis();
            return SystemClock.elapsedRealtime() + msToNextSchedule;
        } else {
            return SystemClock.elapsedRealtime() + time;
        }
    }

    public static String toCommaSeparatedString(List<String> list) {
        final StringBuilder builder = new StringBuilder();
        for (String string : list) {
            builder.append(string);
            builder.append(",");
        }
        builder.deleteCharAt(builder.length() - 1);
        return builder.toString();
    }
}




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