Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;

import android.text.TextUtils;

public class Main {
    static SharedPreferences mPreferences;
    static PackageManager mPm;
    static String[] mPrefValues;

    private static String makeTitle(Cursor result, String packageName, String extNumber) {
        String title = "";
        String prefValue = mPreferences.getString("ext_title_preference" + extNumber, "app_count");
        // App name, no count
        if (prefValue.equals(mPrefValues[0])) {
            try {
                title = mPm.getApplicationLabel(mPm.getApplicationInfo(packageName, 0)).toString();
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        // App name with count
        else if (prefValue.equals(mPrefValues[1])) {
            int count = result.getCount() == 0 ? 0 : result.getInt(10);
            try {
                title = mPm.getApplicationLabel(mPm.getApplicationInfo(packageName, 0)).toString() + " ("
                        + Integer.toString(count) + ")";
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        // Custom, no count
        else if (prefValue.equals(mPrefValues[2])) {
            try {
                String temp = mPreferences.getString("text_preference" + extNumber, "");
                if (TextUtils.isEmpty(temp))
                    title = mPm.getApplicationLabel(mPm.getApplicationInfo(packageName, 0)).toString();
                else
                    title = temp;

            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        // Custom with count
        else if (prefValue.equals(mPrefValues[3])) {
            int count = result.getCount() == 0 ? 0 : result.getInt(10);
            try {
                String temp = mPreferences.getString("text_preference" + extNumber, "");
                if (TextUtils.isEmpty(temp))
                    title = mPm.getApplicationLabel(mPm.getApplicationInfo(packageName, 0)).toString() + " ("
                            + Integer.toString(count) + ")";
                else
                    title = temp + " (" + Integer.toString(count) + ")";

            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        // Notification title, no count
        else if (prefValue.equals(mPrefValues[4])) {
            try {
                title = result.getString(3);
            } catch (CursorIndexOutOfBoundsException e) {
                try {
                    title = mPm.getApplicationLabel(mPm.getApplicationInfo(packageName, 0)).toString();
                } catch (NameNotFoundException e1) {
                    e1.printStackTrace();
                }
            }
        }

        // Notification title with count
        else if (prefValue.equals(mPrefValues[5])) {
            int count = result.getCount() == 0 ? 0 : result.getInt(10);
            try {
                title = result.getString(3) + " (" + Integer.toString(count) + ")";
            } catch (CursorIndexOutOfBoundsException e) {
                try {
                    title = mPm.getApplicationLabel(mPm.getApplicationInfo(packageName, 0)).toString() + " ("
                            + Integer.toString(count) + ")";
                } catch (NameNotFoundException e1) {
                    e1.printStackTrace();
                }
            }

        }

        result.moveToFirst();
        return title;

    }
}