When installing from Google Play, you will receive a broadcast of com.android.vending.INSTALL_REFERRER during the installation process. Just create a broadcast receiver for this and a boolean value in your SharedPreferences in the fromGooglePlay lines, which you can set to true in the receiver.
However, I donโt think you might have similar receivers for other app stores such as Amazon's.
EDIT 1:
As @zapl notes in the comments, this will not work on Android 3.0 and above, since applications that have not been launched at least once are not allowed to receive broadcasts, etc.
However, you can try an alternative solution:
Your application has a boolean constant somewhere, something like GOOGLE_PLAY_BUILD . When creating apk for Google Play, set it to true , and when creating apk for manual distribution, set it to false . Then just check its value at runtime and act accordingly.
EDIT 2:
To solve your problems with the root devices that distribute your applications, here is a more complex system.
Each device has a unique ANDROID_ID (although apparently several users in 4.2 have several, despite what the documentation says). In fact, you just need a unique device identifier, so even the MAC address will work.
You have a table on the web server to store the unique identifier, regardless of whether you received a Google Play broadcast or the value of the Google Play flag. When the application is launched for the first time, add this data for this device to the table.
When the application starts after that, just check if all three details of the copy stored locally match. If the user receives the application from the root user and installs it, the Google Play flag seems to be consistent, however, part of the installation referrer will not. Unfortunately, this will only work on pre-cellular devices.
Or in a system with little trust, you can simply ask the user if they are installed manually or from Google Play. If you mentioned this in order to choose an upgrade method rather than chasing them for installation from outside of Google Play, I feel like theyโll be happy to tell the truth.
EDIT 3
As @Jan noted in the comments, there is a getInstallerPackageName () method in PackageManager that can be used for this purpose.