I am to the point in developing my paid application version where I
need to implement the support for copying the database.

I found that using CONTEXT_IGNORE_SECURITY gets me past the call to
createPackageContext without a SecurityException, but it does not
allow me to access the database files from my free version.  I have
given both my free and paid applications the same shared user id
string, but when I use CONTEXT_INCLUDE_CODE I am still getting a
SecurityException when I call createPackageContext.

In my manifest file for both the free and paid versions of my
application I have set:
   android:sharedUserId="@string/shared_user_id"
and in the strings.xml file I have set:
   <string name="shared_user_id">myappname</string>

However when I call createPackageContext with the parameter
CONTEXT_INCLUDE_CODE it generates the SecurityException:
   Requesting code from com.mydomain.myappnamefree (with uid 10033) to
be run in process com.mydomain.myappnamepaid (with uid 10049)

Since the SecurityException is showing two different uid's it seems
that setting the sharedUserId in the manifest file didn't actually
result in both apps getting the same user id.

The code I am trying to use to copy the database, from my free app,
when my paid app starts up is:

        public static void readPreviousPackageDb(Context myContext)
        {
                File myDbFile = myContext.getDatabasePath(DATABASE_NAME);
                if (myDbFile.exists()) {
                        // Paid app database already exists
                        return;
                }

                Context freeContext = null;
                try {
                        freeContext = myContext.createPackageContext(
                                        FREE_VERSION_PACKAGE_NAME, 
Context.CONTEXT_INCLUDE_CODE);
                        if (freeContext == null) {
                                // Free version is not installed
                                return;
                        }
                } catch (Exception e) {
                        String error = e.getMessage();
                        Log.e(TAG, "Could not copy previous edition database " +
                                        error);
                        return;
                }
                File freeDbFile = freeContext.getDatabasePath(DATABASE_NAME);
                if (!freeDbFile.exists()) {
                        Log.e(TAG, "Free database file is missing");
                        return;
                }
                long freeDbSize = freeDbFile.length();
                Log.v(TAG, "File size is " + freeDbSize + " for file " +
freeDbFile.getAbsolutePath());
                try {
                        //FileInputStream inputStream = new 
FileInputStream(freeDbFile);
                        InputStream inputStream = freeContext.getAssets().open
(freeDbFile.getAbsolutePath());
                        myDbFile.createNewFile();
                        FileOutputStream outputStream = new 
FileOutputStream(myDbFile);
                        byte buffer[] = new byte[2048];
                        long copySize = 0;
                        do {
                                int bytesRead = inputStream.read(buffer);
                                if (bytesRead <= 0) {
                                        break;
                                }
                                outputStream.write(buffer, 0, bytesRead);
                                copySize += bytesRead;
                        } while (true);
                        Log.i(TAG, "Copied " + copySize + " of " + freeDbSize);
                        inputStream.close();
                        outputStream.close();
                } catch (FileNotFoundException e) {
                        String error = e.getMessage();
                        Log.e(TAG, "Can't copy database from previous edition " 
+ error);
                } catch (SecurityException e) {
                        String error = e.getMessage();
                        Log.e(TAG, "Can't copy database from previous edition " 
+ error);
                } catch (IOException e) {
                        String error = e.getMessage();
                        Log.e(TAG, "Can't copy database from previous edition " 
+ error);
                }
        }

I have tried generating a signed version of my app and installing it,
in case the shared user id does not work with unsigned debug code, but
that didn't seem to change the behavior at all.

I would really appreciate some help, as I cannot figure out why my two
versions of the application are getting assigned different uids by
Android when they both have the same shared user id string value.

If I use CONTEXT_IGNORE_SECURITY instead of CONTEXT_INCLUDE_CODE it
gets past the call to createPackageContext without any
SecurityException.  It is able to detect that the free database file
exists and can see the correct size of this file.  But when I call to
create the InputStream it generates a FileNotFoundException.

I am guessing that the problem is because the SecurityException
reports that each application has a different UID.  However since I
specified the same sharedUserId in the manifest file for both
applications I don't understand why they have different UID values.




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

Reply via email to