[Q] Can't hide navigation drawer and take screenshot - Java for Android App Development

Hi, I'm developing a bug-report function in my app and I want to take a screenshot but I first need to hide the navigation drawer in the code.
I wrote and tested the functions to do such things but when I put them together what I get is a screenshot with the drawer open! Here's the code:
Code:
//I call the android drawerLayout method to close it
mDrawerLayout.closeDrawer(Gravity.START);
// and that's how I take the screenshot
public static Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
It's like I need to wait some ms to let the drawer close and then take the screenshot. Any ideas?

Do you have a DrawerToggle integrated in your code? It has an onDrawerClosed method which should be called when the drawer is completely closed. So you could set a field variable to true if a screenshot should be taken and inside the onDrawerClosed you check this variable and if needed call your screenShot method.
--------------------
Phone: Nexus 4
OS: rooted Lollipop LRX21T
Bootloader: unlocked
Recovery: TWRP 2.8.2.0

This method works but I don't like it because I think that using field variables in that way dirty the code. I'm still searching for a better solution but thank you Masrepus! =)
Sent from my GT-I8730 using XDA Free mobile app

Related

[Q] Lockscreen Notifications

Does anyone know if it's possible to mod the Stock Froyo lockscreen to enable access to the notification bar (check notifications from lockscreen)? I know widget locker does this but was wondering if I could edit a system file (android.policy.jar maybe?) to enable this.
I would like to know as well. Love your rom by the way Geo.
I think you're looking for
frameworks/policies/base/phone/com/android/internal/policy/impl/KeyguardViewMediator.java specifically:
Code:
private void adjustStatusBarLocked() {
...
// if the keyguard is shown, allow the status bar to open
// only if the keyguard is insecure and is covered by another window
boolean enable = !mShowing || (mHidden && !isSecure());
mStatusBarManager.disable(enable ?
StatusBarManager.DISABLE_NONE :
StatusBarManager.DISABLE_EXPAND);
navillos said:
I would like to know as well. Love your rom by the way Geo.
Click to expand...
Click to collapse
Thanks
[email protected] said:
I think you're looking for
frameworks/policies/base/phone/com/android/internal/policy/impl/KeyguardViewMediator.java specifically:
Code:
private void adjustStatusBarLocked() {
...
// if the keyguard is shown, allow the status bar to open
// only if the keyguard is insecure and is covered by another window
boolean enable = !mShowing || (mHidden && !isSecure());
mStatusBarManager.disable(enable ?
StatusBarManager.DISABLE_NONE :
StatusBarManager.DISABLE_EXPAND);
Click to expand...
Click to collapse
Can I edit pre-existing files or does this have to compiled from source? I really haven't built from source but this may be the thing that gets me started.
relating to this, does anyone know how to disable the power button while lock screen is on?

[app][root]SecHoleUtils.apk 1.1 Control GPS and PIN from your app. KitKat ready.

Google has always limited the access to several features like GPS on/off management, PIN control and more to third party applications. Sometimes the developers have taken advantage of certain security bugs to circonvent those limitations, but Google has fixed those bugs on the latest OS version.
There are certain functions that in my opinion should be allowed because the security risk is minimun, like GPS control and SIM PIN entering , so I've wr1tten a small app to open this possibility. It does nothing by itself and is not normally loaded but registers for specific intents and when received does those forbidden functions.
Any thir party application can take advantage of it, no need to ask for permisions. Simply broadcast and intent and voila!
Sample usage code:
Code:
public static final String TOOGLE_GPS = "com.beemer.secholeutils.TOOGLE_GPS";
public static final String ENTER_PIN = "com.beemer.secholeutils.ENTER_PIN";
public static final String EXTRA_PIN = "com.beemer.secholeutils.EXTRA_PIN";
public void toogleGPS() {
Intent intent = new Intent(TOOGLE_GPS);
sendBroadcast(intent);
}
public void enterPIN(String PIN) {
Intent intent = new Intent(ENTER_PIN);
intent.putExtra(EXTRA_PIN, PIN);
sendBroadcast(intent);
}
Disclaimer
I'm nor responsible of what happens to your device if you install this apk or while you install it.
Installing this app could allow any third party app to use it to turn on/off the GPS or enter your SIM PIN (if known) even try an brute force atack without asking for permission.
The app must be installed as a system app. You can do it manually but from kitkat on it has became somewhat difficult. I recomend installing busybox and systemappmover from Play store:
https://play.google.com/store/apps/details?id=stericson.busybox
https://play.google.com/store/apps/details?id=de.j4velin.systemappmover
Install secHoleUtils.apk as a normal app and use SystemAppMover to make it a system app.
DEBUG
In order to debug your app, SecHoleUtils will log the calls and errors if they happens:
Code:
pre-KitKat
Log.i("SecHoleUtils", "Received " + TOOGLE_GPS + " intent");
Kitkat:
Log.d("SecHoleUtils", "ToogleGPS. Current mode: " + currentMode + " newMode: " + newMode);
Log.e("SecHoleUtils", "Error toogling GPS");
Log.i("SecHoleUtils", "Received " + ENTER_PIN + " intent");
Log.e("SecHoleUtils", "Error inserting PIN");
Detection of the APK
In order to detect in your app if SecHoleUtils is installed and you can use it, there is a sample code:
Code:
//Test if SecHoleUtils.apk is installed
boolean bSecHoleUtils = false;
try {
getPackageManager().getPackageInfo("com.beemer.secholeutils",PackageManager.GET_META_DATA);
bSecHoleUtils = true;
}
catch (NameNotFoundException e1){}//Not installed. No need to do anything.
If I see interest in the app, I'll add more APIs to it.

2G/3G Preferred Network Mode with root?

I'm trying to access the hidden API for Phone.class to switch the preferred network mode via reflection.
I know this is not safe for porting to other android versions/roms and requires a system app to hold permission WRITE_SECURE_SETTINGS.
I Already managed to switch data via reflection and toggle gps via Settings.Secure.putString() by moving my app to system partition, so i think this should work too.
Does anyone have experience with reflection and/or root and is willing to help?
Of course I will share all my code/findings here.
Thanks in advance!
Links:
http://stackoverflow.com/questions/8607263/android-4-0-4g-toggle
http://stackoverflow.com/questions/5436251/how-to-access-setpreferrednetworktype-in-android-source
You can achieve this with a simple sqlite3 query instead of bothering with java reflection
Here's how :
In a terminal :
To enable 2G only
Code:
su
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
insert into global values(null, 'preferred_network_mode', 1);
.exit
To enable 2G & 3G :
Code:
su
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
insert into global values(null, 'preferred_network_mode', 0);
.exit
In java :
Example to enable 2G only (just replace the 1 with a 0 in the insert into to enable 2G & 3G)
Code:
try {
Process process = null;
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(
process.getOutputStream());
os.writeBytes("sqlite3 /data/data/com.android.providers.settings/databases/settings.db" + "\n");
os.writeBytes("insert into global values(null, 'preferred_network_mode', 1);" + "\n");
os.writeBytes(".exit" + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
It might require a reboot to be active though, not sure
If it doesn't work on your rom (tested on cm10.1), it might be a different row name, try investigating your settings.db like this :
Code:
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
.tables [COLOR="Green"]// returns all the tables[/COLOR]
select * from [I]tablename[/I]; [COLOR="green"]// in cm10.1 it's in [I]global[/I] but otherwise look in [I]secure[/I] and/or [I]system[/I]
// this command returns all the rows from the selected table, the names are usually explicit, so you should find the setting you're looking for[/COLOR]
Then, when you found it :
Code:
insert into [I]tablename[/I] values(null, [I]'row name'[/I], value);
Thanks, this is very helpful!
I think you are right, one will need to tell the system to update the settings or send a broadcast that this setting has changed (like for airplane mode).
I will look at the CM sources to find out what i can do.
superkoal said:
Thanks, this is very helpful!
I think you are right, one will need to tell the system to update the settings or send a broadcast that this setting has changed (like for airplane mode).
I will look at the CM sources to find out what i can do.
Click to expand...
Click to collapse
Glad it helped.
If you manage to find a way to make the settings read the db without a reboot, please share your findings, would be very useful.
Androguide.fr said:
Glad it helped.
If you manage to find a way to make the settings read the db without a reboot, please share your findings, would be very useful.
Click to expand...
Click to collapse
Of course i will!
Buddy... you can toggle network mode with intent, no root needed, nor permissions!!
Here´s entire sample:
Code:
package com.serajr.togglenetworkmode;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.provider.Settings.SettingNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bnt = (Button) findViewById(R.id.button1);
bnt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// toggle
toggleNetworkMode();
}
});
}
private void toggleNetworkMode() {
int mode = getCurrentNetworkMode() + 1;
if (mode > 2) {
mode = 0;
}
// 0 = 3G_ONLY
// 1 = 3GSM_ONLY
// 2 = 3G_PREFERRED
// change mode
Intent intent = new Intent("com.android.phone.CHANGE_NETWORK_MODE");
intent.putExtra("com.android.phone.NEW_NETWORK_MODE", mode);
sendBroadcast(intent);
}
private int getCurrentNetworkMode() {
try {
int current = Secure.getInt(getContentResolver(), "preferred_network_mode");
return current;
} catch (SettingNotFoundException ex) {
return 0;
}
}
}
Try it and tell me later!!!
Thanks, but this will only work if your phone.apk has an exported receiver for this intent.
This is only the case if you or your rom dev modded it to be so.
Some custom ROMs also have this kind of "mod/bug" in the power widget, allowing you to toggle gps.
I tried it with slim bean 4.2 build 3 on i9000 and as expected it didn't work (and so will most likely in CM10.1 too).
Just too good to be true!
If youre following the root method Use RootTools for that
sak-venom1997 said:
If youre following the root method Use RootTools for that
Click to expand...
Click to collapse
Thanks, I already do.
Really helpful library!
And yes, I will do it by sql injection to settings.db
I only need to figure out what broadcasts have to be sent / methods called and if this is possible.
Argh I am kinda lost in CM sources.
Can anyone point me to the place where this is handled?
I managed to toggle flight mode by using this in a SU shell:
Code:
if(settingenabled)
{
executeCommand("settings put global airplane_mode_on 1");
executeCommand("am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true");
}
else
{
executeCommand("settings put global airplane_mode_on 0");
executeCommand("am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false");
}
Interstingly enough it didn't work if i manually injected the value and then sent the broadcast.
I have no idea why.
So atm I'm trying to do the same thing for preferred_network_mode. I can write the value to settings.db, but I just don't know what i have to do to make the system apply the setting yet.
EDIT: shell binary "settings" only works in android 4.2 (and probably upwards)
So i finally found it in CM Sources.
Seems like we need to get the currently running Instance of the Phone class to notify it about the change.
Also seems like we are back to reflection, as this is an internal system class.
I also killed com.android.phone process after updating the setting, but that didn't change anything.
EDIT:
1st step towards reflection:
I figured out that i need a jar file from the cm build to use system classes via reflection.
Does anyone have more information about this?
superkoal said:
So i finally found it in CM Sources.
Seems like we need to get the currently running Instance of the Phone class to notify it about the change.
Also seems like we are back to reflection, as this is an internal system class.
I also killed com.android.phone process after updating the setting, but that didn't change anything.
EDIT:
1st step towards reflection:
I figured out that i need a jar file from the cm build to use system classes via reflection.
Does anyone have more information about this?
Click to expand...
Click to collapse
Just a far fetched guess, but maybe try to add /system/framework/framework.jar from your rom to your app's build path
Is a reboot that unacceptable in your case ?
Maybe it's just the challenge, but i wanna change it like every other setting
A reboot is very far from what i call comfortable
superkoal said:
Maybe it's just the challenge, but i wanna change it like every other setting
A reboot is very far from what i call comfortable
Click to expand...
Click to collapse
Yeah, I get what you mean^^^
Here's a good (but old, 2009^^) official Android tutorial from the Android devs on reflection if it can help you : http://android-developers.blogspot.fr/2009/04/backward-compatibility-for-android.html
So i got some news
1. This setting is heavily protected by android system and can only be modified by the phone process itself
2. Some roms have a modified phone apk listening to a broadcast, enabling 3rd party apps to toggle
http://forum.xda-developers.com/showthread.php?t=1731187
3. His Majesty Chainfire started an app project which could amongst other features also toggle 2g/3g, but he gave up development. Regarding 2g/3g he implemented several methods, one of them being RIL injection, which he described as a really hard hack and highly experimental.
http://forum.xda-developers.com/showthread.php?t=807989
Seems this task is not that easy
But i found out that tasker can toggle 2g/3g on my phone (running slim bean 4.2.2), this is when i found out about the modified phone.apk from 2). So I'mcomfortable that it's working on my phone and i can at least develop an app for myself
Sent from my GT-I9000 using xda app-developers app

ArrayList problem with If(position=.....)

Hello!
I have a little problem.
I had EditText filter, Array List(with Listview), and every position from list is opening new Activity(with some text) by "if(position==0,1,2,3...)"
But when i search with filter some text, and single rows are changing position after search, Activity called "Chicken" is geting a wrong String(from String.xml)
I know the problem is cause i use "if" with position, but i dont know how to solve that.
I don't even know what i should write to uncle google, Please help
using viewholder?
Use view.setTag(someText) on every list item
Then when it is clicked call getTag() to get the string you should pass to the activity
--------------------
Phone: Nexus 4
OS: rooted Lollipop LRX21T
Bootloader: unlocked
Recovery: TWRP 2.8.2.0
Maybe the problem you don't clear filtered list whenever you add new letter to EditText ??
if so, Use filteredList.clear(); once the text changed.
You have to call notify() on the listview or in the ListView adaper to tell the list that changes have been done.
GeknGekn said:
You have to call notify() on the listview or in the ListView adaper to tell the list that changes have been done.
Click to expand...
Click to collapse
I'm not sure whether notify() will work as well, I only know of notifyDatasetChanged(), which I already tested
--------------------
Phone: Nexus 4
OS: rooted Lollipop LRX21T
Bootloader: unlocked
Recovery: TWRP 2.8.2.0

How can I make ChiralCode - Android-Color-Picker appear in a dialog window?

I found the following link 'https://github.com/chiralcode/Android-Color-Picker'. I copied `ColorPicker.java' and `ColorPickerDialog.java' . I've been trying to make that dialog box appear somehow .. but I still can't figure it out. If you run the initial app you can see that it has a color picker dialog button (when you press it that window appears). How can I make something like that?
I tried using this(as he says on that website) :
ColorPickerDialog colorPickerDialog = new ColorPickerDialog(this, initialColor, new OnColorSelectedListener() {
@override
public void onColorSelected(int color) {
// do action
}
});
colorPickerDialog.show();
but I get a lot of errors. I only copied those 2 java files, nothing else. Not even the layout or something like that. What else should I copy in order to make it work ? I'm using Android Studio
I've been trying to solve this for a long time .. but however I try I won't get it done. Thank you
iDaniel19 said:
I found the following link 'https://github.com/chiralcode/Android-Color-Picker'. I copied `ColorPicker.java' and `ColorPickerDialog.java' . I've been trying to make that dialog box appear somehow .. but I still can't figure it out. If you run the initial app you can see that it has a color picker dialog button (when you press it that window appears). How can I make something like that?
I tried using this(as he says on that website) :
ColorPickerDialog colorPickerDialog = new ColorPickerDialog(this, initialColor, new OnColorSelectedListener() {
@override
public void onColorSelected(int color) {
// do action
}
});
colorPickerDialog.show();
but I get a lot of errors. I only copied those 2 java files, nothing else. Not even the layout or something like that. What else should I copy in order to make it work ? I'm using Android Studio
I've been trying to solve this for a long time .. but however I try I won't get it done. Thank you
Click to expand...
Click to collapse
You should probably copy all the files there... if any aren't needed Android Studio will tell you as they won't be used.
Edit: Also you're doing it wrong, have a look at the demo code here & here.
Thank you for that. I will try it now. How should I add the color picker to my layout ?
Edit: I added it to my layout . Using the first demo I get the same values by clicking the button. I tried the second one, but it's a lot more complicated, and I'm getting nowhere. Thank you .
Edit2 : Thank you.I did it. I couldn't have done it without your help.

Categories

Resources