Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;

public class Main {
    public static ArrayList<long[]> getRanking(Activity activity, String mode2) {
        ArrayList<long[]> rankList = new ArrayList<long[]>();

        String rankRecord = getStoredVar(activity, "ranking_" + mode2);
        if (rankRecord.equals("") == false) {
            String[] records = rankRecord.split(",");
            for (int i = 0; i < records.length; i += 2) {
                long[] l = { Long.valueOf(records[i]), Long.valueOf(records[i + 1]), 0 };
                rankList.add(l);
            }
        }

        return rankList;
    }

    public static ArrayList<long[]> getRanking(Activity activity, String mode2, long newRecord, long currentTime) {
        ArrayList<long[]> rankList = new ArrayList<long[]>();

        String rankRecord = getStoredVar(activity, "ranking_" + mode2);
        if (rankRecord.equals("") == false) {
            String[] records = rankRecord.split(",");
            boolean inserted = false;
            for (int i = 0; i < records.length; i += 2) {
                if (inserted == false && (Long.valueOf(records[i]) / 1000) > (newRecord / 1000)) {
                    long[] l = { newRecord, currentTime, 1 };
                    rankList.add(l);
                    long[] l2 = { Long.valueOf(records[i]), Long.valueOf(records[i + 1]), 0 };
                    rankList.add(l2);
                    inserted = true;
                } else {
                    long[] l = { Long.valueOf(records[i]), Long.valueOf(records[i + 1]), 0 };
                    rankList.add(l);
                }
                if (rankList.size() == 5) {
                    break;
                }
            }
            if (inserted == false && rankList.size() < 5) {
                long[] l = { newRecord, currentTime, 1 };
                rankList.add(l);
            }
        } else {
            long[] l = { newRecord, currentTime, 1 };
            rankList.add(l);
        }

        String newRank = "";
        for (int i = 0; i < rankList.size(); i++) {
            if (newRank.equals("") == false) {
                newRank += ",";
            }
            newRank += (new Long(rankList.get(i)[0])).toString() + "," + (new Long(rankList.get(i)[1])).toString();
        }
        if (newRank.equals("") == false) {
            setStoredVar(activity, "ranking_" + mode2, newRank);
        }

        return rankList;
    }

    public static String getStoredVar(Activity activity, String key) {
        SharedPreferences settings = activity.getSharedPreferences(key, Context.MODE_PRIVATE);
        return settings.getString(key, "");
    }

    public static void setStoredVar(Activity activity, String key, String val) {
        SharedPreferences settings = activity.getSharedPreferences(key, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, val);
        editor.commit();
    }
}