4

I have created an encrypted .obb file using the jobb tool. I use the following code to mount the obb file:

    public void mountExpansion() {
    final StorageManager storageManager  = (StorageManager) getContext()
        .getSystemService(Context.STORAGE_SERVICE);


    String packageName = "name.of.the.package";
    String filePath = Environment.getExternalStorageDirectory()
            + "/Android/obb/" + packageName + "/" + "main."
            + version + "." + packageName + ".obb";
    final File mainFile = new File(filePath);
    if (mainFile.exists()) {
        Log.d("STORAGE", "FILE: " + filePath + " Exists");
    } else {
        Log.d("STORAGE", "FILE: " + filePath + " DOESNT EXIST");
    }

    String key = "thisIsMyPassword";
    if (!storageManager.isObbMounted(mainFile.getAbsolutePath())) {
        if (mainFile.exists()) {
            if(storageManager.mountObb(mainFile.getAbsolutePath(), key,
                    new OnObbStateChangeListener() {
                        @Override
                        public void onObbStateChange(String path, int state) {
                            super.onObbStateChange(path, state);
                            Log.d("PATH = ",path);
                            Log.d("STATE = ", state+"");
                            expansionFilePath = storageManager.getMountedObbPath(path);
                            if (state == OnObbStateChangeListener.MOUNTED) {
                                expansionFilePath = storageManager
                                        .getMountedObbPath(path);
                                Log.d("STORAGE","-->MOUNTED");
                            } 
                            else {
                                Log.d("##", "Path: " + path + "; state: " + state);
                            }
                        }
                    }))
            {
                Log.d("STORAGE_MNT","SUCCESSFULLY QUEUED");
            }
            else
            {
                Log.d("STORAGE_MNT","FAILED");
            }

        } else {
            Log.d("STORAGE", "Patch file not found");
        }
    }



}

I am getting the following output: FILE: filePath Exists SUCCESSFULLY QUEUED

But nothing inside onObbStateChangeListener is getting called. I am calling this function from a custom view and testing this on Nexus 4/ KitKat.

What could be the reason for this behaviour?

manlio
  • 18,345
  • 14
  • 76
  • 126
chochim
  • 1,710
  • 5
  • 17
  • 30

2 Answers2

7

I know this question is old but this may help someone else.

The StorageManager stores the listener in a weak reference which means that, given your example code (an anonymous instance created in the method call), it is gone almost as soon as you create it and usually well before the mount completes. You must maintain a reference to the listener object in your own code until it is no longer needed.

Something like this should work:

public class MyClass {

    ...
    private OnObbStateChangeListener mListener =
                new OnObbStateChangeListener() {
                    @Override
                    public void onObbStateChange(String path, int state) {
                       // your code here
                    }
                };

    public void mountExpansion() {
        ...
        if (storageManager.mountObb(mainFile.getAbsolutePath(), key, mListener)
        {
            Log.d("STORAGE_MNT","SUCCESSFULLY QUEUED");
        }
        else
        {
            Log.d("STORAGE_MNT","FAILED");
        }
        ...
     }
     ...
}

This particular quirk of obb mounting has existed since at least honeycomb to my knowledge.

pcash
  • 71
  • 1
  • 2
  • after mount obb file can i see all my resource files in the dir /Android/obb/(Package Name) then, how can i use my resource files in my project – Krunal Shah Sep 18 '15 at 10:54
  • I'm facing a similar situation (see my similar question: http://stackoverflow.com/questions/37997042/unable-to-mount-obb-file-created-with-jobb) and I tried what is suggested in this answer and still get the state to be 20. Have you since found a solution for this? – ez4nick Jun 24 '16 at 00:40
  • This should be the accepted answer! This problem has been torturing me for a while now and it's just random; sometimes it works, sometimes it fails. Being the subject of an indeterministic GC makes the most sense. I've yet to see the results (since it's random) but if it fails I'll report again. – hasen Dec 29 '17 at 11:30
1

There seems to be a bug with OBB mounting that was introduced with KitKat. Currently no workarounds are known however it should be fixed with the next incremental update.

http://code.google.com/p/android/issues/detail?id=61881

step_jac
  • 161
  • 4