Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    static float memsize = -1.0f;

    public static boolean LargerMemoryMode(Context context) {
        if (true) {
            if (memsize == -1.0f) {
                memsize = getMemeorySize(context);
                Log.d("memory", "size = " + memsize);
            }
            return memsize >= 2.75f;//1.75, just for x7
        }

        return true;
    }

    private static float getMemeorySize(Context context) {
        if (Build.VERSION.SDK_INT >= 16) {
            ActivityManager actManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
            actManager.getMemoryInfo(memInfo);
            float totalMemory = (memInfo.totalMem / 1073741824.0f);
            return totalMemory;
        } else {
            return getTotalRAM();
        }
    }

    public static float getTotalRAM() {
        RandomAccessFile reader = null;
        String load = null;
        DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
        double totRam = 0;
        float lastValue = 0;
        try {
            reader = new RandomAccessFile("/proc/meminfo", "r");
            load = reader.readLine();

            // Get the Number value from the string
            Pattern p = Pattern.compile("(\\d+)");
            Matcher m = p.matcher(load);
            String value = "";
            while (m.find()) {
                value = m.group(1);
                // System.out.println("Ram : " + value);
            }
            reader.close();

            totRam = Double.parseDouble(value);
            // totRam = totRam / 1024;

            float mb = (float) (totRam / 1024.0f);
            float gb = (float) (totRam / 1048576.0f);
            float tb = (float) (totRam / 1073741824.0f);

            lastValue = gb;
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            // Streams.close(reader);
        }

        return lastValue;
    }
}