FileObserver not working for /proc/net/tcp6 - android

I want to monitor /proc/net/tcp6 file and to do that efficiently with FileObserver, however for some unknown reason onEvent() callback never called.
observerTcp6 = new FileObserver("/proc/net/tcp6", FileObserver.ALL_EVENTS) {
#Override
public void onEvent(int event, String path) {
Log.i("TAG", "onEvent");
}
};
observerTcp6.startWatching();
With regular File class parsing this files works perfect.
Could anyone help me here? :)

The reason of your failure is that /proc/net/tcp6 is not really a file ;-)
It only looks like a file (ex., you can "open" and "read" it), but actually whole /proc/* entries are an interfaces to various kernel statistics\data, represented as "pseudo-files" only for simplifying access to them.
So, you can not use any other file methods on them, except "open" and "read".
P.S.
Your question is Linux related one, not actually Android.

Related

What is happening in my Android app to cause it to lose data?

I have an android app. I have a few users who have a recurring problem: When the app shuts down, every file the app saved is gone. Every folder created is gone. Everything is completely wiped back to square one.
I am carefully saving the game data during every transition and game event, so I am very confident that this is not a case of the user crashing out before the data can be written. Somehow, the data that IS being written but then it's just not persisting after the app is removed from memory.
So-- has anyone had this situation and solved it? The only thing I can imagine is that there's some kind of "filesystem.commit" command I need to call after writing the files, but I can't find that documented anywhere.
Please help!
(Edit) I'm using native code to read and write files. The code I use to write a file is this:
bool WriteFile(char *theFilename, char *theDataPtr, int theLen)
{
FILE* aFile=fopen(theFilename,"w+");
if(!aFile) {Alert("unable to create file %s with error %d", theFilename, errno);return false;}
if(aFile) fclose(aFile);
aFile=fopen(theFilename,"w+b");
if(!aFile) {Alert ("unable to open file %s", theFilename);return false;}
if (aFile)
{
fwrite(theDataPtr, 1, theLen,aFile);
fclose(aFile);
return true;
}
return false;
}
Note:No customers are reporting any alert popups, which are just normal Android message boxes. Also note that this code works on almost every other system-- there's just a few customers that get the wiped data, so I was wondering if it's some weird security or some extra step I need to do to be 100% compatible with all systems.
(Edit) One more piece of information... this is the Java code I use to get the storage path for the app... all files that I try to write are put in this folder.
private void SetFilePath()
{
String storagePath = getFilesDir().getAbsolutePath();
// SDCARD
try {
String storageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(storageState))
storagePath = getExternalFilesDir(null).getAbsolutePath();
} catch (Exception e) {
Log.v(IDS.LOG,
"No permission to access external storage, missing android.permission.WRITE_EXTERNAL_STORAGE");
}
SetFilePathNative(storagePath); // Tells the native code the path
mStorageDir = storagePath;
}

Actual behavior of DevicePolicyManager.setApplicationHidden

Is there anyone knows SDK well enough to tell why after making my app device-owner and executing DevicePolicyManager.setApplicationHidden for a list of apps I can't then find them unless I'd put PackageManager.getInstalledApplications(PackageManager.GET_META_DATA or PackageManager.MATCH_UNINSTALLED_PACKAGES) in the query flags? (meaning just GET_META_DATA won't work and hidden applications would not be unhidden)
I try to find them to unhide them — but no success unless I am looking for uninstalled also.
My theory is that Android doesn't really hide them, but actually marking them uninstalled yet keeping all the data — but I didn't find any information regards it in the documentation. Wonder if someone can clarify this behavior (with source).
Thanks!
The naming is ambiguous but indeed setApplicationHidden effectively uninstalls the app but keeps the APK and data in storage. If you're looking for a way to prevent use of the app without uninstalling it you can try setPackagesSuspended.
You can find the source for setApplicationHidden in PackageManagerService.setApplicationHiddenSettingAsUser() (relevant abstract below). Its logic is simple:
setApplicationHiddenSettingAsUser calls packageSetting.setHidden() to mark the package as hidden for the user,
other methods of PackageManagerService call packageSetting.getHidden() to decide whether the package should be returned.
Code abstract for setApplicationHiddenSettingAsUser:
#Override
public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
int userId) {
PackageSetting pkgSetting;
...
try {
...
synchronized (mPackages) {
pkgSetting = mSettings.mPackages.get(packageName);
...
if (pkgSetting.getHidden(userId) != hidden) {
pkgSetting.setHidden(hidden, userId);
mSettings.writePackageRestrictionsLPr(userId);
...
}
}
...
}
}

cn1 - download file to phone's download directory

I'm trying to allow an app to download files to the public 'Downloads' directory so it's available on the device in a generic fashion. I succeeded in downloading the files in the simulator to the .cn1 directory on my computer, but I couldn't find a straightforward way to get the file into a public directory on the device. I really expected there to be a method in the FileSystemStorage class that would allow this, but none of them seem to be what I'm looking for.
So I tried writing a simple native bridge to get the path to the public directory, starting with Android. I have a very simple class that looks like this:
public class DownloadDirectoryImpl {
public static String getDownloadDirectory(){
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
}
public boolean isSupported(){
return true;
}
}
The app compiles fine, but when I click on the file to download it, I see the same line repeating itself in the logcat a few thousand times:
W/System.err: at net.gesher.downloadDirectory.NativeDownloadDirectoryImpl.getDeviceDownloadDirectory(NativeDownloadDirectoryImpl.java:20)
But it doesn't state what the error is.
So, I'm looking for advice either a) to improve my android code so that it works, or b) the cn1 proper way of getting this directory path.
Thanks a ton!
If you have a lot of lines saying at... and all refer to the same method then you have a recursive call leading to a stack overflow. Since the only method you mentioned is your native method I'm assuming you called your own method within the native implementation and got into a recursive loop.

Why FileObserver not working for /data/anr/traces.txt?

Android application have read/write permission for /data/anr/traces.txt. But still FileObserver does not seem to work for it.
What else is required for FileObserver to work? It works fine for sdcard file.
Code:
mFileObserver = new FileObserver("/data/anr/traces.txt") { // set up a file observer to
#Override
public void onEvent(int event, String file) {
if(event == FileObserver.CLOSE_WRITE)
{
Log.e("TestApp", "ANR has occured");
}
}
};
mFileObserver.startWatching();
Found solution to it therefore answering my own question as it may help others.
Looks like using complete file path "/data/anr/traces.txt" is not working. But using path to "/data/anr" is working ok.
Still not sure why complete path doesn't work.
Before calling mFileObserver.startWatching(), you should make sure the file exists. Or no events will be notified.

Creating a log trace of the application life cycle

I have created an application that extensively requires user inputs and interaction and even though I have made sure that I test and catch every possible case that might throw an error I want to be able to create a mechanism that traces the error in case my application crashes on the field.
I want to be able to record the entire flow right from a button click till whatever the user might be selecting or the navigation between the pages in a log file such that in case my application crashes I'm able to study the trace file later and know exactly where the error occurred.
I'm very new to this sort of programming and therefore any pointers on the above will be very helpful! Thank you in advance :]
PS: I'm not even sure whether what im referring to will be correctly called a "log trace" or not so any edit is welcome. :)
EDIT : I also want to be able to save the error report generated and send it to a particular id (similar to 'send an error report to xyz).
UPDATE :
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try {
File myFiles = new File("/sdcard/ScanApp");
if(!myFiles.exists())
{
myFiles.mkdirs();
}
File myFile = new File("sdcard/ScanApp/log.txt");
myFile.createNewFile();
myFile.delete();
myFile.createNewFile();
String cmd = "logcat -d -v time -f "+myFile.getAbsolutePath()+ " -s ActivityManager:V";
Runtime.getRuntime().exec(cmd);
Logs.this.finish();
}
catch (Exception e)
{
flag=1;
error=e.getMessage();
}
I used this in a previous application for recording any application activity and make a textfile and save it to the SD card, but the contents weren't exactly what I was looking for. Is the solution im looking for something along these lines?
Here, check for the link for reference.
In here you create a class say ExceptionHandler that implements java.lang.Thread.UncaughtExceptionHandler..
Inside this class you will do your life saving stuff like creating stacktrace and gettin ready to upload error report etc....
Now comes the important part i.e. How to catch that exception.
Though it is very simple. Copy following line of code in your each Activity just after the call of super method in your overriden onCreate method.
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));
Your Activity may look something like this…
public class ForceClose extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));
setContentView(R.layout.main);
}
}
Hope this helps...
You need to look up on Exception Handling. That is when your application crashes or any other app level errors occur, the code in the exception block executes. So in that place, log that error in a text-file and which solves your "log trace" issue.
Refer the link for beautiful examples.

Categories

Resources