Autoincrement VersionCode Gradle task not executing as expected - android

I wanted to automate android gradle versioning my requirement is
Ver Code Version Name
So i have runned code in java application
Code:
public static void main(String[] args) {
int fileVersionCode = 1;
String fileVersionName;
for (int i = 0; i < 200; i++) {
if (fileVersionCode <= 10) {
fileVersionName = "1." + (fileVersionCode - 1);
} else if(fileVersionCode>=20 && fileVersionCode%10 ==0) {
fileVersionName = ( (fileVersionCode / 10)) + ".9";
}else {
fileVersionName = (1 + (fileVersionCode / 10)) + "." +( (fileVersionCode % 10)-1);
}
System.out.println(fileVersionCode+" "+fileVersionName);
fileVersionCode++;
}
}
And my code is working as expected but in gradle for the same code
task firstTask {
doFirst {
int fileVersionCode = 1;
String fileVersionName;
for (int i = 0; i < 200; i++) {
if (fileVersionCode <= 10) {
fileVersionName = "1." + (fileVersionCode - 1);
} else if(fileVersionCode>=20 && fileVersionCode%10 ==0) {
fileVersionName = ( (fileVersionCode / 10)) + ".9";
}else {
fileVersionName = (1 + (fileVersionCode / 10)) + "." +( (fileVersionCode % 10)-1);
}
System.out.println(fileVersionCode+" "+fileVersionName);
fileVersionCode++;
}
}
doLast{
// println 'firstTask doLast'
}
}
Run :> gradlew firstTask
output is different why i am getting like this

In groovy a division results in a BigDecimal if the operands are of type Integer, while in Java they are of the type int.
When fileVersionCode is 11, the result:
(fileVersionCode / 10)
translates to 1 in Java but to 1.1 in Groovy. In order to fix this, just add a (int) cast to trim the unnecessary decimal part.

Related

Google Map blink in Android TV when open for long time

I have android TV app which will run for long time .And I have google map inside that but after 20-24 hours google map starts blinking. Following link contains video of issue.
https://www.dropbox.com/s/vp8pbqc5z4zopbz/20180611_095004.mp4?dl=0
Edit
I can't share the whole source code, but I am using two fragments. One fragment contains map and another contains a listview of images. I call a webservice every 10 seconds and update listview images and bound map with locations I get in response of web service.
if ( map!=null) {
if (activity.ambDetailList.size() > 0) {
int i;
double dist, currentLat = 0.0, currentLng = 0.0;
ambOnlineList.clear();
String ETA, SPEED;
Marker marker;
for (i = 0; i < activity.ambDetailList.size(); i++) {
ambulanceDetail = activity.ambDetailList.get(i);
dist = Math.sqrt(Math.pow(ambulanceDetail.getCurrentLat() - ambulanceDetail.getDestLat(), 2) + Math.pow(ambulanceDetail.getCurrentLng() - ambulanceDetail.getDestLng(), 2));
double minute = ((dist * 100) / (ambulanceDetail.getSpeed() * 60 / 1000));
double speed = ambulanceDetail.getSpeed() * 60 * 60 / 1000;
Log.i("Minute", "----->>>>" + minute);
if (speed >= 0.00 && speed <= 1.00) {
ETA = activity.getResources().getString(R.string.text_infinity);
SPEED = activity.getResources().getString(R.string.text_infinity);
} else if (minute > 60) {
double hour = minute / 60;
int roundHour = (int) (minute / 60);
int min = (int) ((hour - roundHour) * 60);
ETA = roundHour + " hour " + min + " min";
SPEED = (int) (ambulanceDetail.getSpeed() * 60 * 60 / 1000) + " km/h ";
} else {
ETA = (int) (minute) + " min ";
SPEED = (int) (ambulanceDetail.getSpeed() * 60 * 60 / 1000) + " km/h ";
}
ambulanceDetail.setEta(ETA);
ambulanceDetail.setDisplayETA(ETA);
ambulanceDetail.setDisplaySpeed(SPEED);
if (markerList.get(ambulanceDetail.getAmbulanceId()) == null) {
MarkerOptions markerOptions = createMarker(ambulanceDetail);
marker = map.addMarker(markerOptions);
markerList.put(ambulanceDetail.getAmbulanceId(), marker);
markerArrayList.add(marker);
detailMap.put(marker, ambulanceDetail);
} else {
marker = markerList.get(ambulanceDetail.getAmbulanceId());
if (marker.isVisible()) {
Location location = new Location("");
location.setLatitude(ambulanceDetail.getCurrentLat());
location.setLongitude(ambulanceDetail.getCurrentLng());
marker.setIcon(BitmapDescriptorFactory.fromBitmap(updateMarkerIcon(ambulanceDetail)));
marker.setPosition(new LatLng(ambulanceDetail.getCurrentLat(), ambulanceDetail.getCurrentLng()));
}
}
ArrayList<Integer> listId = new ArrayList<>();
for (int m = 0; m < activity.ambDetailList.size(); m++) {
listId.add(activity.ambDetailList.get(m).getAmbulanceId());
}
for (int k = 0; k < markerArrayList.size(); k++) {
Marker m = markerArrayList.get(k);
if (!listId.contains(detailMap.get(m).getAmbulanceId())) {
markerArrayList.remove(m);
markerList.remove(detailMap.get(m).getAmbulanceId());
detailMap.remove(m);
m.remove();
}
}
}
if (markerList.size() == 1 && activity.ambDetailList.size() == 1) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(markerList.get(activity.ambDetailList.get(0).getAmbulanceId()).getPosition(), 16));
} else {
boundLatLang();
}
if (isFirstCall) {
map.setBuildingsEnabled(true);
isFirstCall = false;
}
} else {
if (markerArrayList.size() > 0) {
for (int i = 0; i < markerArrayList.size(); i++) {
Marker marker = markerArrayList.get(i);
marker.remove();
}
markerList.clear();
markerArrayList.clear();
}
map.clear();
if (detailMap.size() > 0)
detailMap.clear();
}
}
Anyway, you can restart application (or if that didn't helps - reboot device) before map starts blinking, for example after 15 hours of work (or e.g. 500 calls of web service):
...
Intent restartIntent = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(restartIntent);
...

Prime Factorization in Android

I am developing a small Prime Number application for Android devices and am nearly done, however I would like some help with optimizing my factorization class.
I am still having one or two problems with some large numbers(Even Numbers) being factored within a reasonable amount of time. I won't be able to use the sieve of Eratosthenes for this particular project I think as I can only sieve up to 10 million without the app crashing on my physical device (Samsung Galaxy S4 Mini). So my work around algorithm is below. I am not sure if I can maybe make the Pollard Rho algorithm that I implemented any better.
Once I have established that the number being tested isn't prime or isn't a prime square, I quickly do trial division up to 10 000, after that if the number still isn't factored completely I use the Pollard Rho method to reduce it the rest of the way.
I want to be able to factor numbers in the range of 2 > 2^64.
This is an example of a number taking roughly 15 seconds 256332652145852
It's factorization is [2, 2, 1671053, 38348971].
Any help would be gladly appreciated.
try {
long num = Long.valueOf(input);
if(num == 1) {
return "1" + " = " + input;
} else if(num < 1) {
return "Cannot factor a number less than 1";
} else if(PrimeNumbers.isPrime(num) == true) {
return result = num + " is a Prime Number.";
} else if(isSquare(num) == true && PrimeNumbers.isPrime((long) Math.sqrt(num)) == true) {
return result = (int) Math.sqrt(num) + "<sup><small>" + 2 + "</small></sup>" + " = " + input;
} else {
factors(num, pFactors);
return result = exponentialForm(pFactors, num) + " = " + input;
}
} catch(NumberFormatException e) {
return result = "Unfortunately the number entered is too large";
}
}
public static void factors(long n, ArrayList<Long> arr) {
long number = trialDiv(n, arr);
if(number > 1) {
while(true) {
long divisor = pollard(number, 1);
if(PrimeNumbers.isPrime(divisor) == true) {
number /= divisor;
arr.add(divisor);
if(PrimeNumbers.isPrime(number) == true) {
arr.add(number);
break;
}
}
}
}
}
private static long trialDiv(long n, ArrayList<Long> arr) {
while(n % 2 == 0) {
n /= 2;
arr.add((long) 2);
}
for(long i = 3; i < 10000; i += 2) {
if(PrimeNumbers.isPrime(i) == true) {
while(n % i == 0) {
arr.add(i);
n /= i;
}
}
}
if(PrimeNumbers.isPrime(n) == true) {
arr.add(n);
return 1;
}
return n;
}
public static long pollard(long n, long c) {
long x = 2;
long y = 2;
long d = 1;
while (d == 1) {
x = g(x, n, c);
y = g(g(y, n, c), n, c);
d = gcd(Math.abs(y - x), n);
}
if (d == n) {
return pollard(n, c + 1);
} else {
return d;
}
}
static long g(long x, long n, long c) {
long g = (((x * x) + c) % n);
return g;
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
Your pollard function is okay but not great. You are using Pollard's original algorithm, but it would be better to use Brent's variant. But that's probably not the source of your slow performance.
Your trial division function is slow. Checking each possible divisor for primality is very expensive, and not necessary. It doesn't matter if you divide by a composite; the division will always fail, but you don't waste the time checking primality. A better approach is wheel factorization.

Gregorian to Hijri (Islamic) date

Is there any way or library in Android to convert Gregorian date to Hijri(Islamic) one? I found some Java libraries such as Joda Time but I need something which works in Android.
I found this but I didn't test it yet
public class DateHijri {
static double gmod(double n, double m) {
return ((n % m) + m) % m;
}
static double[] kuwaiticalendar(boolean adjust) {
Calendar today = Calendar.getInstance();
int adj = 0;
if (adjust) {
adj = 0;
} else {
adj = 1;
}
if (adjust) {
int adjustmili = 1000 * 60 * 60 * 24 * adj;
long todaymili = today.getTimeInMillis() + adjustmili;
today.setTimeInMillis(todaymili);
}
double day = today.get(Calendar.DAY_OF_MONTH);
double month = today.get(Calendar.MONTH);
double year = today.get(Calendar.YEAR);
double m = month + 1;
double y = year;
if (m < 3) {
y -= 1;
m += 12;
}
double a = Math.floor(y / 100.);
double b = 2 - a + Math.floor(a / 4.);
if (y < 1583)
b = 0;
if (y == 1582) {
if (m > 10)
b = -10;
if (m == 10) {
b = 0;
if (day > 4)
b = -10;
}
}
double jd = Math.floor(365.25 * (y + 4716))
+ Math.floor(30.6001 * (m + 1)) + day + b - 1524;
b = 0;
if (jd > 2299160) {
a = Math.floor((jd - 1867216.25) / 36524.25);
b = 1 + a - Math.floor(a / 4.);
}
double bb = jd + b + 1524;
double cc = Math.floor((bb - 122.1) / 365.25);
double dd = Math.floor(365.25 * cc);
double ee = Math.floor((bb - dd) / 30.6001);
day = (bb - dd) - Math.floor(30.6001 * ee);
month = ee - 1;
if (ee > 13) {
cc += 1;
month = ee - 13;
}
year = cc - 4716;
double wd = gmod(jd + 1, 7) + 1;
double iyear = 10631. / 30.;
double epochastro = 1948084;
double epochcivil = 1948085;
double shift1 = 8.01 / 60.;
double z = jd - epochastro;
double cyc = Math.floor(z / 10631.);
z = z - 10631 * cyc;
double j = Math.floor((z - shift1) / iyear);
double iy = 30 * cyc + j;
z = z - Math.floor(j * iyear + shift1);
double im = Math.floor((z + 28.5001) / 29.5);
if (im == 13)
im = 12;
double id = z - Math.floor(29.5001 * im - 29);
double[] myRes = new double[8];
myRes[0] = day; // calculated day (CE)
myRes[1] = month - 1; // calculated month (CE)
myRes[2] = year; // calculated year (CE)
myRes[3] = jd - 1; // julian day number
myRes[4] = wd - 1; // weekday number
myRes[5] = id; // islamic date
myRes[6] = im - 1; // islamic month
myRes[7] = iy; // islamic year
return myRes;
}
static String writeIslamicDate() {
String[] wdNames = { "Ahad", "Ithnin", "Thulatha", "Arbaa", "Khams",
"Jumuah", "Sabt" };
String[] iMonthNames = { "Muharram", "Safar", "Rabi'ul Awwal",
"Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira", "Rajab",
"Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja" };
// This Value is used to give the correct day +- 1 day
boolean dayTest = true;
double[] iDate = kuwaiticalendar(dayTest);
String outputIslamicDate = wdNames[(int) iDate[4]] + ", " + iDate[5]
+ " " + iMonthNames[(int) iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}
}
You can use this Class in your Project (Hope it helps) :
import java.text.SimpleDateFormat;
import java.util.Date;
public class PersianDate {
public String todayShamsi()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String curentDateandTime = sdf.format(new Date());
String year = curentDateandTime.substring(0, 4);
String month = curentDateandTime.substring(4, 6);
String day = curentDateandTime.substring(6, 8);
int Y = Integer.valueOf(year);
int M = Integer.valueOf(month);
int D = Integer.valueOf(day);
return Shamsi(Y, M, D);
}
public static String Shamsi(int Y, int M, int D)
{
if (Y == 0)
Y = 2000;
if (Y < 100)
Y = Y + 1900;
if (Y == 2000)
{
if (M > 2)
{
SimpleDateFormat temp = new SimpleDateFormat("yyyyMMdd");
String curentDateandTime = temp.format(new Date());
String year = curentDateandTime.substring(0, 4);
String month = curentDateandTime.substring(4, 6);
String day = curentDateandTime.substring(6, 8);
Y = Integer.valueOf(year);
M = Integer.valueOf(month);
D = Integer.valueOf(day);
}
}
if (M < 3 || (M == 3 && D < 21))
{
Y -= 622;
}
else Y -= 621;
switch (M)
{
case 1: if (D < 21)
{
M = 10;
D = D + 10;
}
else
{
M = 11;
D -= 20;
}
break;
case 2: if (D < 20)
{
M = 11;
D = D + 11;
}
else
{
M = 12;
D -= 19;
}
break;
case 3:
if (D < 21)
{
M = 12;
D = D + 9;
}
else
{
M = 1;
D -= 20;
}
break;
case 4:
if (D < 21)
{
M = 1;
D = D + 11;
}
else
{
M = 2; D = D - 20;
}
break;
case 5:
if (D < 22)
{
M = M - 3;
D = D + 10;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 6:
if (D < 22)
{
M = M - 3;
D = D + 10;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 7:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 8:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 9:
if (D < 23)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 22;
}
break;
case 10:
if (D < 23)
{
M = 7;
D = D + 8;
}
else
{
M = 8;
D = D - 22;
}
break;
case 11:
if (D < 22)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 21;
}
break;
case 12:
if (D < 22)
{
M = M - 3;
D = D + 9;
}
else
{
M = M - 2;
D = D - 21;
}
break;
}
String month = "00";
String day = "00";
//D = Integer.valueOf(D)+1;
if (M < 10)
{
month = "0" + M;
}
else
{
month = String.valueOf(M);
}
if (D < 10)
{
day = "0" + D;
}
else
{
day = String.valueOf(D);
}
return String.valueOf(Y) + "/" + month + "/" + day;
}
}

Android Keeping Track of Button Selections

I have written some code in java that will guess a number based on the user's selections. It's pretty simple code. I am trying to integrate this code into an android app, by displaying the different number set each on different pages. The user will then click "Yes" if their number is in the set, or "No" if it isn't, and they'll then be redirected to another page.
The problem is I don't know how to keep track of their selections across the pages so that I can run the "If" statements based on their values as shown in the original code. Is there a simple way to keep track of whether they click "Yes" or "No" across the pages?
This is my java code
public class BinaryGame
{
public static void main (String []args)
{
String set1, set2, set3, set4, set5, another = "y";
int num, num1, num2, num3, num4, rand, number = 0, count = 0;
final int PER_LINE = 4;
Scanner scan = new Scanner (System.in);
Random generator = new Random();
while (another.equalsIgnoreCase("y"))
{
System.out.println ("Think of a number between 0 and 31. And I will be able to figure it out.");
System.out.println ("");
rand = generator.nextInt(2);
if (rand == 1)
{
for (num = 16; num < 32; num++)
{
System.out.print(num + " ");
count++;
if (count % PER_LINE ==0)
System.out.println ();
}
System.out.print ("Is your number in this group? y/n: ");
set1 = scan.nextLine();
if (set1.equalsIgnoreCase("y"))
number += 16;
else
number += 0;
}
else
{
for (num = 0; num < 16; num++)
{
System.out.print(num + " ");
count++;
if (count % PER_LINE ==0)
System.out.println ();
}
System.out.print ("Is your number in this group? y/n: ");
set1 = scan.nextLine();
if (set1.equalsIgnoreCase("y"))
number += 0;
else
number += 16;
}
System.out.println ();
rand = generator.nextInt(2);
if (rand == 1)
{
for (num1 = 8; (num1 < 16) || (23 < num1 && num1 < 32) ; num1++)
{
System.out.print (num1 + " ");
count++;
if (count % PER_LINE ==0)
System.out.println ();
if (num1 == 15)
num1 = 23;
}
System.out.print ("Is your number in this group? y/n: ");
set2 = scan.nextLine();
if (set2.equalsIgnoreCase("y"))
number += 8;
else
number += 0;
}
else
{
for (num1 = 0; (num1 < 8) || (15 < num1 && num1 < 24) ; num1++)
{
System.out.print (num1 + " ");
count++;
if (count % PER_LINE ==0)
System.out.println ();
if (num1 == 7)
num1 = 15;
}
System.out.print ("Is your number in this group? y/n: ");
set2 = scan.nextLine();
if (set2.equalsIgnoreCase("y"))
number += 0;
else
number += 8;
}
System.out.println ();
rand = generator.nextInt(2);
if (rand == 1)
{
for (num2 = 4; (num2 < 8) || (11 < num2 && num2 < 16) || (19 < num2 && num2 < 24) || (27 < num2 && num2 < 32); num2++)
{
System.out.print (num2 + " ");
count++;
if (count % PER_LINE ==0)
System.out.println ();
if (num2 == 7)
num2 = 11;
if (num2 == 15)
num2 = 19;
if (num2 == 23)
num2 = 27;
}
System.out.print ("Is your number in this group? y/n: ");
set3 = scan.nextLine();
if (set3.equalsIgnoreCase("y"))
number += 4;
else
number += 0;
}
else
{
for (num2 = 0; (num2 < 4) || (7 < num2 && num2 < 12) || (15 < num2 && num2 < 20) || (23 < num2 && num2 < 28); num2++)
{
System.out.print (num2 + " ");
count++;
if (count % PER_LINE ==0)
System.out.println ();
if (num2 == 3)
num2 = 7;
if (num2 == 11)
num2 = 15;
if (num2 == 19)
num2 = 23;
}
System.out.print ("Is your number in this group? y/n: ");
set3 = scan.nextLine();
if (set3.equalsIgnoreCase("y"))
number += 0;
else
number += 4;
}
System.out.println ();
rand = generator.nextInt(2);
if (rand == 1)
{
for (num3 = 2; (num3 < 4) || (5 < num3 && num3 < 8) || (9 < num2 && num3 < 12) || (13 < num3 && num3 < 16) ||
(17 < num3 && num3 < 20) || (21 < num3 && num3 < 24) || (25 < num3 && num3 < 28) || (29 < num3 && num3 < 32); num3++)
{
System.out.print (num3 + " ");
count++;
if (count % PER_LINE ==0)
System.out.println ();
if (num3 == 3)
num3 = 5;
if (num3 == 7)
num3 = 9;
if (num3 == 11)
num3 = 13;
if (num3 == 15)
num3 = 17;
if (num3 == 19)
num3 = 21;
if (num3 == 23)
num3 = 25;
if (num3 == 27)
num3 = 29;
}
System.out.print ("Is your number in this group? y/n: ");
set4 = scan.nextLine();
if (set4.equalsIgnoreCase("y"))
number += 2;
else
number += 0;
}
else
{
for (num3 = 0; (num3 < 2) || (3 < num3 && num3 < 6) || (7 < num2 && num3 < 10) || (11 < num3 && num3 < 14) ||
(15 < num3 && num3 < 18) || (19 < num3 && num3 < 22) || (23 < num3 && num3 < 26) || (27 < num3 && num3 < 30); num3++)
{
System.out.print (num3 + " ");
count++;
if (count % PER_LINE ==0)
System.out.println ();
if (num3 == 1)
num3 = 3;
if (num3 == 5)
num3 = 7;
if (num3 == 9)
num3 = 11;
if (num3 == 13)
num3 = 15;
if (num3 == 17)
num3 = 19;
if (num3 == 21)
num3 = 23;
if (num3 == 25)
num3 = 27;
}
System.out.print ("Is your number in this group? y/n: ");
set4 = scan.nextLine();
if (set4.equalsIgnoreCase("y"))
number += 0;
else
number += 2;
}
System.out.println ();
rand = generator.nextInt(2);
if (rand == 1)
{
for (num4 = 1; num4 < 32; num4 += 2)
{
System.out.print (num4 + " ");
count++;
if (count % PER_LINE ==0)
System.out.println ();
}
System.out.print ("Is your number in this group? y/n: ");
set5 = scan.nextLine();
if (set5.equalsIgnoreCase("y"))
number += 1;
else
number += 0;
}
else
{
for (num4 = 0; num4 < 32; num4 += 2)
{
System.out.print (num4 + " ");
count++;
if (count % PER_LINE ==0)
System.out.println ();
}
System.out.print ("Is your number in this group? y/n: ");
set5 = scan.nextLine();
if (set5.equalsIgnoreCase("y"))
number += 0;
else
number += 1;
}
System.out.println ();
System.out.println ("You chose: " + number);
number = 0;
System.out.print ("Would you like to play again? y/n: ");
another = scan.nextLine();
}
}
}
If by pages you mean Acvities with in the same application, then you would want to look at SharedPreferences (class reference and dev guide).
In the Activity where the button is pressed, you might want to do somethinge like:
SharedPreferences prefs = getSHaredPreferecne("buttonPrefs");
SharedPreferences.Editor editor = prefs.editor();
editor.PutBoolean("btnYes", true);
editor.commit();
Then, when you need access this (again with in the same application):
SharedPreferecnes myPrefs = getShharedPreferences("buttonPrefs")
boolean yesIsPressed = myPrefs.getBoolean("btnYes", false) // the second param is a default return value if preference is not found
SharedPreferences can used anywhere in tha same application, as long you have the correct name.

Issue with WifiManager.calculateSignalLevel(RSSI, 5)

I am trying to use the Wifimanager to calculate the Signal Level of the access points found during a scan.
I am using the following method:
WifiManager.calculateSignalLevel(int, int)
But it appears to always return the same int no matter what the RSSI level is.
Here is my code:
public int calculateQoS(int aRSSI){
signalLevel = WifiManager.calculateSignalLevel(RSSI, 5);
return signalLevel;
}
public void testCalculateQoS(){
Log.d("signal", "signal = : "
+ connMonitor.calculateQoS(-44)
+ " " + connMonitor.calculateQoS(-80)
+ " " + connMonitor.calculateQoS(-120)
+ " " + connMonitor.calculateQoS(-20));
}
The logging outputs 1 for all the test cases for calculateQoS(int).
Am I missing something simple here? Why is the SignalLevel always 1?
It seems that calculateSignalLevel is implemented this way:
public static int calculateSignalLevel(int rssi, int numLevels) {
if (rssi <= MIN_RSSI) {
return 0;
} else if (rssi >= MAX_RSSI) {
return numLevels - 1;
} else {
int partitionSize = (MAX_RSSI - MIN_RSSI) / (numLevels - 1);
return (rssi - MIN_RSSI) / partitionSize;
}
}
Maybe this code snippet can explain your problem. Also note:
http://code.google.com/p/android/issues/detail?id=2555
thanks to this question I could prevent problem on lower API versions then the one I'm targetting. So I made this so you can use it on any platform version.
public int getWifiSignalStrength(Context context){
int MIN_RSSI = -100;
int MAX_RSSI = -55;
int levels = 101;
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
int rssi = info.getRssi();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
return WifiManager.calculateSignalLevel(info.getRssi(), levels);
} else {
// this is the code since 4.0.1
if (rssi <= MIN_RSSI) {
return 0;
} else if (rssi >= MAX_RSSI) {
return levels - 1;
} else {
float inputRange = (MAX_RSSI - MIN_RSSI);
float outputRange = (levels - 1);
return (int)((float)(rssi - MIN_RSSI) * outputRange / inputRange);
}
}
}//end method
This issue is only in android 2.3,
you can replace it with latest code of WiFiManger of an android 4.2
Here is the code:
public int calculateSignalLevel(int rssi, int numLevels) {
if(rssi <= MIN_RSSI) {
return 0;
} else if(rssi >= MAX_RSSI) {
return numLevels - 1;
} else {
float inputRange = (MAX_RSSI - MIN_RSSI);
float outputRange = (numLevels - 1);
if(inputRange != 0)
return (int) ((float) (rssi - MIN_RSSI) * outputRange / inputRange);
}
return 0;
}

Categories

Resources