I finally figured out how to copy the database from my free
application to the paid application.

After rebuilding numerous times and testing many different
combinations I discovered the following:

1) It doesn't matter if I use the debug signing key or my release
signing key, as long as both the free and the paid versions are signed
with the same key.  This means that I can test with the debug version
and step through the code just fine if both versions have been signed
with the debug signing key.

2) Specifying a sharedUserId using a reference to a @string does NOT
work.  Even though both the free and the paid versions were built with
the same @string being specified for the sharedUserId Android always
assigned them different UIDs and threw a SecurityException when I
called createPackageContext with CONTEXT_INCLUDE_CODE.  Specifying the
sharedUserId WITHOUT using @string does work.

Using:
   android:sharedUserId="myappname"
for both programs it would end up with both the free and the paid
versions of the application running with the same UID.

I think the source of my confusion here was the instructions given in
the Android reference documentation for the sharedUserLabel:

"android:sharedUserLabel   A user-readable label for the shared user
ID. The label must be set as a reference to a string resource; it
cannot be a raw string."

Apparently the sharedUserId MUST be a raw string, and the
sharedUserLabel MUST NOT be a raw string.  It would have saved me a
lot of time if this was clearly documented in the reference.  If you
are going to be inconsistent, please clearly document the inconsistent
behavior.

3) The database belonging to the free application must be opened using
the the paid application context.  The following code does NOT work:
  InputStream inputStream = freeContext.getAssets().open
(freeDbFile.getAbsolutePath());
But this code does work:
  FileInputStream inputStream = new FileInputStream(freeDbFile);

4) The directory where the new database file needs to be copied to
does not exist, and the directory must be created before the database
can be created.

Putting this all together, the code which works to copy the database
is:

public static void readPreviousPackageDb(Context myContext)
{
        File myDbFile = myContext.getDatabasePath(DATABASE_NAME);
        if (myDbFile.exists()) {
                // My database already exists
                return;
        }
        try {
                Context freeContext = myContext.createPackageContext(
                                LITE_EDITION_PACKAGE_NAME, 
Context.CONTEXT_INCLUDE_CODE);
                File freeDbFile = freeContext.getDatabasePath(DATABASE_NAME);
                if (!freeDbFile.exists()) {
                        Log.e(TAG, "Free database file is missing");
                        return;
                }
                FileInputStream inputStream = new FileInputStream(freeDbFile);
                File myDbDirectory = new File(myDbFile.getParent());
                if (!myDbDirectory.isDirectory() && !myDbDirectory.mkdirs()) {
                        inputStream.close();
                        return;
                }
                myDbFile.createNewFile();
                FileOutputStream outputStream = new FileOutputStream(myDbFile);
                byte buffer[] = new byte[2048];
                do {
                        int bytesRead = inputStream.read(buffer);
                        if (bytesRead <= 0) {
                                break;
                        }
                        outputStream.write(buffer, 0, bytesRead);
                } while (true);
                inputStream.close();
                outputStream.close();
                Log.i(TAG, "Successfully copied database from the free 
version");
        } catch (Exception e) {
                Log.i(TAG, "Could not copy database from free version " +
e.getMessage());
        }
}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to