Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

Well mreed, I'm starting to think that this must be a bug or just not possible.  App sees sd card no problem when clicked on but will not see sd card at auto run when device restarts.  I've exhausted all sorts of efforts in getting this to work.  Do you have any further thoughts?  Can you get it to work yourself?  Really apprecate everything mreed.  Thank you.

Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Detect SDCARD on App Startup

Which startup teir are you in? Maybe try a later one? Not sure what it could be unless its just something silly like you have the device plugged in or something. I let the rim devs know about this thread, so hopefully they can chime in.

Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

[ Edited ]

Thanks mreed.  I've tried both 6 and 7 and still wont work.  I've stripped out all code in the app and tried even the most basic code and still the app does not see the sc card after device restarts, only when clicked on.  Here is the very basic code I'm trying and not working...

 

Also doesnt seem to matter if device plugged in or not.

 

Took out .inStartup() because it didnt seem to matter at all.

 

package ma.MyApp;

import java.util.Enumeration;
import javax.microedition.io.file.FileSystemRegistry;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

public class MyApp extends UiApplication {

	public static void main(String[] args) {
		MyApp app = new MyApp();
        app.enterEventDispatcher();
	}

	public MyApp() {
		pushScreen(new MyAppMainScreen());
	}

	public static void errorDialog(final String message) {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			public void run() {
				Dialog.alert(message);
			} 
		});
	}
	
	public class MyAppMainScreen extends MainScreen {
		
		boolean sdCardPresent = false;

		public MyAppMainScreen() { 

			setTitle("myapp");

			String root = null;
			Enumeration e = FileSystemRegistry.listRoots();
			while (e.hasMoreElements()) {
				root = (String) e.nextElement();
				if( root.equalsIgnoreCase("sdcard/") ) {
					sdCardPresent = true;
				} 
			}
			if(!sdCardPresent) {
				MyApp.errorDialog("no sd card, exiting");
				System.exit(0);
			}
		}
	}

}

 

 

Are you able to make this work?

 

Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Detect SDCARD on App Startup

[ Edited ]

I haven't run it. I don't have a device with me right now.

 

Also make sure its not a permissions issue. Just set everything to allow for now.

 

Try this and see what this prints out:

 

package mypackage;

import java.util.Enumeration;

import javax.microedition.io.file.FileSystemRegistry;

import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.SystemListener2;
import net.rim.device.api.system.USBPort;
import net.rim.device.api.ui.UiApplication;

public class MyApp extends UiApplication {

    public static void main( String[] args ) {

        final MyApp theApp = new MyApp();
        theApp.invokeLater( new Runnable() {

            public void run() {

                theApp.bootloader();
            }
        } );
        theApp.enterEventDispatcher();
    }

    private void bootloader() {

        ApplicationManager applicationManager = ApplicationManager.getApplicationManager();
        boolean inStartup = applicationManager.inStartup();

        System.out.println( "Device Startup: " + ( inStartup ? "RUNNING" : "FINISHED" ) );

        if ( !inStartup ) {
            checkFilesystem();
        }
        else {

            new Thread( new Runnable() {

                public void run() {

                    waitForStartup();
                    checkFilesystem();
                }
            } ).start();
        }
    }

    private void checkFilesystem() {

        boolean usbConnected = false;
        boolean massStorageEnabled = false;
        boolean sdcardPresent = false;

        int state = USBPort.getConnectionState();
        if ( ( SystemListener2.USB_STATE_ENUMERATED & state ) == state ) {
            usbConnected = true;
            if ( ( SystemListener2.USB_STATE_MS_INTERFACE_ENUMERATED & state ) == state ) {
                massStorageEnabled = true;
            }
        }

        Enumeration roots = FileSystemRegistry.listRoots();
        while (roots.hasMoreElements()) {
            String root = (String) roots.nextElement();
            System.out.println( "Found Root: " + root );
            if ( root.equals( "SDCard/" ) ) {
                sdcardPresent = true;
            }
        }

        System.out.println( "USB: " + ( usbConnected ? "" : "NOT " ) + "CONNECTED" );
        System.out.println( "Mass Storage Mode: " + ( massStorageEnabled ? "ENABLED" : "DISABLED" ) );
        System.out.println( "SDCard: " + ( sdcardPresent ? "" : "NOT " ) + "FOUND" );
    }

    private void waitForStartup() {

        ApplicationManager applicationManager = ApplicationManager.getApplicationManager();
        while (applicationManager.inStartup()) {
            try {
                Thread.sleep(100);
            }
            catch (Throwable error) {
                // nothing
            }
        }
    }
}

 

Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

[ Edited ]

Thanks mreed.  I really appreciate all of your help in resolving this issue.  You've been fantastic.  Thank you.  I've Tried the exact code however I am unable to get any results at all after device restarts.  App seems to stall device booting for a very long time, when the device finally does come back up the app just wont run... locks up.

 

When debugging using device I do receive...

[164.968] USB: NOT CONNECTED
[164.968] Mass Storage Mode: DISABLED
[164.968] SDCard: FOUND

 

However, app does lock up and cause issues as noted above after devcice restarts.

Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Detect SDCARD on App Startup

Probably from the yield. I'll run it when i get home.

Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Detect SDCARD on App Startup

Hi KJake,

 

A couple of questions:

 

1. When you call the 'listRoots' function in the 'onStartupWork', what values do you get?

 

2. Have you tried just creating a file on the SDCard (at 'onStartupWork') and catch the error? If you do manage to create a file, then a SD card exists. If not, an should be thrown and a SD card does not exists.

 

Hope that helps,

 

E.

Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

[ Edited ]

Thank for the reply.  After restart I get "path does not contains a proper root list" error.  However, when opening the app again it runs fine.  Must be something to do with tming after restart.

 

This must be a bug somewhere in the OS... I've done everything I can think of...

Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Detect SDCARD on App Startup

[ Edited ]

Okay, so I got a chance to run it on the device, and it looks like they changed something in how the SDCard is loaded up (or maybe they didn't). My device is running 7.0.0. Looping until out of startup used to work fine (as far as I remember... its been a while). The SystemListener2.powerUp() also does not work because the SDCard doesn't get added until sometime after startup (about 10 seconds according to the log). So checking it when power up ends is still too soon.

 

However, lets go back to FileSystemListener. You said you tried that. Silly question, but did you actually add the listener to the FileSystemRegistry? Because thats working for me. See code below:

 

package mypackage;

import java.util.Enumeration;

import javax.microedition.io.file.FileSystemListener;
import javax.microedition.io.file.FileSystemRegistry;

import net.rim.device.api.system.Application;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.EventLogger;
import net.rim.device.api.system.SystemListener2;

public class MyApp extends Application implements SystemListener2, FileSystemListener {

    public static void main( String[] args ) {

        EventLogger.register( MyApp.class.hashCode(), "MyApp", EventLogger.VIEWER_STRING );

        final MyApp theApp = new MyApp();
        theApp.invokeLater( new Runnable() {

            public void run() {

                theApp.bootloader();
            }
        } );
        theApp.enterEventDispatcher();
    }

    private void bootloader() {

        ApplicationManager applicationManager = ApplicationManager.getApplicationManager();
        boolean inStartup = applicationManager.inStartup();

        print( "Device Startup: " + ( inStartup ? "RUNNING" : "FINISHED" ) );

        if ( !inStartup ) {
            checkFilesystem();
        }
        else {
            addSystemListener( this );
            FileSystemRegistry.addFileSystemListener( this );
        }
    }

    private void print( String str ) {

        System.out.println( str );
        EventLogger.logEvent( MyApp.class.hashCode(), str.getBytes(), EventLogger.INFORMATION );
    }

    private void checkFilesystem() {

        boolean sdcardPresent = false;

        Enumeration roots = FileSystemRegistry.listRoots();
        while (roots.hasMoreElements()) {
            String root = (String) roots.nextElement();
            print( "Found Root: " + root );
            if ( root.equals( "SDCard/" ) ) {
                sdcardPresent = true;
            }
        }

        print( "SDCard: " + ( sdcardPresent ? "" : "NOT " ) + "FOUND" );

        if ( sdcardPresent ) {
            FileSystemRegistry.removeFileSystemListener( this );
        }
    }

    public void powerUp() {

        print( "Power up complete" );
        checkFilesystem();
        removeSystemListener( this );
    }

    public void rootChanged( int state, String rootName ) {

        String stateStr;
        if ( state == FileSystemListener.ROOT_ADDED ) {
            stateStr = "ROOT_ADDED";
        }
        else if ( state == FileSystemListener.ROOT_REMOVED ) {
            stateStr = "ROOT_REMOVED";
        }
        else {
            stateStr = "UNKNOWN";
        }
        print( "Root changed: state[" + stateStr + "], name[" + rootName + "]" );
        checkFilesystem();
    }

    public void usbConnectionStateChange( int state ) {}
    public void batteryGood() {}
    public void batteryLow() {}
    public void batteryStatusChange( int status ) {}
    public void powerOff() {}
    public void backlightStateChange( boolean on ) {}
    public void cradleMismatch( boolean mismatch ) {}
    public void fastReset() {}
    public void powerOffRequested( int reason ) {}
}

 

In the event logger I see this (paraphrasing):

... device startup: running

... power up complete

... store/ and system/ available, but not sdcard

... root changed: sdcard added

... sdcard found

Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

[ Edited ]

Thank you very much, mreed.  Really appreciate all of your help.  I've just tried this code on my side and all it does it lock up the devices... a 9900 running 7.1.  Could this be because no screen is displayed?  Can it be modigied to display a screen if/when sdcar nd and a diferent screen displayed if not found?

 

I do get sam results as you.

 

I can ee sdcard when manually launching app just not after device restarts and app auto runs.