[
  {
    "path": ".gitignore",
    "content": "Library/\r\nPackages/\r\nProjectSettings/\r\nsrc/unityOBBDownloader/.gradle/\r\nsrc/unityOBBDownloader/build/\r\nTemp/\r\nUnityPackageManager/\r\n*.meta\r\n*.csproj\r\n*.sln\r\n.vs/\r\n"
  },
  {
    "path": "Assets/Scripts/DownloadObbExample.cs",
    "content": "using UnityEngine;\nusing System.Collections;\n\npublic class DownloadObbExample : MonoBehaviour\n{\n    private IGooglePlayObbDownloader m_obbDownloader;\n    void Start()\n    {\n        m_obbDownloader = GooglePlayObbDownloadManager.GetGooglePlayObbDownloader();\n        m_obbDownloader.PublicKey = \"\"; // YOUR PUBLIC KEY HERE\n    }\t\n\n    void OnGUI()\n    {\n        if (!GooglePlayObbDownloadManager.IsDownloaderAvailable())\n        {\n            GUI.Label(new Rect(10, 10, Screen.width-10, 20), \"Use GooglePlayDownloader only on Android device!\");\n            return;\n        }\n        \n        string expPath = m_obbDownloader.GetExpansionFilePath();\n        if (expPath == null)\n        {\n                GUI.Label(new Rect(10, 10, Screen.width-10, 20), \"External storage is not available!\");\n        }\n        else\n        {\n            var mainPath = m_obbDownloader.GetMainOBBPath();\n            var patchPath = m_obbDownloader.GetPatchOBBPath();\n            \n            GUI.Label(new Rect(10, 10, Screen.width-10, 20), \"Main = ...\"  + ( mainPath == null ? \" NOT AVAILABLE\" :  mainPath.Substring(expPath.Length)));\n            GUI.Label(new Rect(10, 25, Screen.width-10, 20), \"Patch = ...\" + (patchPath == null ? \" NOT AVAILABLE\" : patchPath.Substring(expPath.Length)));\n            if (mainPath == null || patchPath == null)\n                if (GUI.Button(new Rect(10, 100, 100, 100), \"Fetch OBBs\"))\n                    m_obbDownloader.FetchOBB();\n        }\n\n    }\n}\n"
  },
  {
    "path": "Assets/Scripts/GooglePlayDownloader.cs",
    "content": "using UnityEngine;\nusing System;\n\npublic interface IGooglePlayObbDownloader\n{\n    string PublicKey { get; set; }\n\n    string GetExpansionFilePath();\n    string GetMainOBBPath();\n    string GetPatchOBBPath();\n    void FetchOBB();\n}\n\npublic class GooglePlayObbDownloadManager\n{\n    private static AndroidJavaClass m_AndroidOSBuildClass = new AndroidJavaClass(\"android.os.Build\");\n    private static IGooglePlayObbDownloader m_Instance;\n\n    public static IGooglePlayObbDownloader GetGooglePlayObbDownloader()\n    {\n        if (m_Instance != null)\n            return m_Instance;\n\n        if (!IsDownloaderAvailable())\n            return null;\n\n        m_Instance = new GooglePlayObbDownloader();\n        return m_Instance;\n    }\n\n    public static bool IsDownloaderAvailable()\n    {\n        return m_AndroidOSBuildClass.GetRawClass() != IntPtr.Zero;\n    }\n}\n"
  },
  {
    "path": "Assets/Scripts/GooglePlayDownloaderImpl.cs",
    "content": "using UnityEngine;\nusing System.IO;\nusing System;\n\ninternal class GooglePlayObbDownloader : IGooglePlayObbDownloader\n{\n    private static AndroidJavaClass EnvironmentClass = new AndroidJavaClass(\"android.os.Environment\");\n    private const string Environment_MediaMounted = \"mounted\";\n\n    public string PublicKey { get; set; }\n\n    private void ApplyPublicKey()\n    {\n        if (string.IsNullOrEmpty(PublicKey))\n        {\n            Debug.LogError(\"GooglePlayObbDownloader: The public key is not set - did you forget to set it in the script?\\n\");\n        }\n        using (var downloaderServiceClass = new AndroidJavaClass(\"com.unity3d.plugin.downloader.UnityDownloaderService\"))\n        {\n            downloaderServiceClass.SetStatic(\"BASE64_PUBLIC_KEY\", PublicKey);\n            // Used by the preference obfuscator\n            downloaderServiceClass.SetStatic(\"SALT\", new byte[] { 1, 43, 256 - 12, 256 - 1, 54, 98, 256 - 100, 256 - 12, 43, 2, 256 - 8, 256 - 4, 9, 5, 256 - 106, 256 - 108, 256 - 33, 45, 256 - 1, 84 });\n        }\n    }\n\n    public void FetchOBB()\n    {\n        ApplyPublicKey();\n        using (var unityPlayerClass = new AndroidJavaClass(\"com.unity3d.player.UnityPlayer\"))\n        {\n            var currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>(\"currentActivity\");\n            var intent = new AndroidJavaObject(\"android.content.Intent\",\n                                                currentActivity,\n                                                new AndroidJavaClass(\"com.unity3d.plugin.downloader.UnityDownloaderActivity\"));\n\n            const int Intent_FLAG_ACTIVITY_NO_ANIMATION = 0x10000;\n            intent.Call<AndroidJavaObject>(\"addFlags\", Intent_FLAG_ACTIVITY_NO_ANIMATION);\n            intent.Call<AndroidJavaObject>(\"putExtra\", \"unityplayer.Activity\",\n                                                        currentActivity.Call<AndroidJavaObject>(\"getClass\").Call<string>(\"getName\"));\n            try\n            {\n                currentActivity.Call(\"startActivity\", intent);\n            }\n            catch (Exception ex)\n            {\n                Debug.LogError(\"GooglePlayObbDownloader: Exception occurred while attempting to start DownloaderActivity - is the AndroidManifest.xml incorrect?\\n\" + ex.Message);\n            }\n        }\n    }\n\n    private string m_ExpansionFilePath;\n    public string GetExpansionFilePath()\n    {\n        if (EnvironmentClass.CallStatic<string>(\"getExternalStorageState\") != Environment_MediaMounted)\n        {\n            m_ExpansionFilePath = null;\n            return m_ExpansionFilePath;\n        }\n\n        if (string.IsNullOrEmpty(m_ExpansionFilePath))\n        {\n            const string obbPath = \"Android/obb\";\n            using (var externalStorageDirectory = EnvironmentClass.CallStatic<AndroidJavaObject>(\"getExternalStorageDirectory\"))\n            {\n                var externalRoot = externalStorageDirectory.Call<string>(\"getPath\");\n                m_ExpansionFilePath = string.Format(\"{0}/{1}/{2}\", externalRoot, obbPath, ObbPackage);\n            }\n        }\n        return m_ExpansionFilePath;\n    }\n\n    public string GetMainOBBPath()\n    {\n        return GetOBBPackagePath(GetExpansionFilePath(), \"main\");\n    }\n\n    public string GetPatchOBBPath()\n    {\n        return GetOBBPackagePath(GetExpansionFilePath(), \"patch\");\n    }\n\n    private static string GetOBBPackagePath(string expansionFilePath, string prefix)\n    {\n        if (string.IsNullOrEmpty(expansionFilePath))\n            return null;\n\n        var filePath = string.Format(\"{0}/{1}.{2}.{3}.obb\", expansionFilePath, prefix, ObbVersion, ObbPackage);\n        return File.Exists(filePath) ? filePath : null;\n    }\n\n    private static string m_ObbPackage;\n    private static string ObbPackage\n    {\n        get\n        {\n            if (m_ObbPackage == null)\n            {\n                PopulateOBBProperties();\n            }\n            return m_ObbPackage;\n        }\n    }\n\n    private static int m_ObbVersion;\n    private static int ObbVersion\n    {\n        get\n        {\n            if (m_ObbVersion == 0)\n            {\n                PopulateOBBProperties();\n            }\n            return m_ObbVersion;\n        }\n    }\n\n    // This code will reuse the package version from the .apk when looking for the .obb\n    // Modify as appropriate\n    private static void PopulateOBBProperties()\n    {\n        using (var unityPlayerClass = new AndroidJavaClass(\"com.unity3d.player.UnityPlayer\"))\n        {\n            var currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>(\"currentActivity\");\n            m_ObbPackage = currentActivity.Call<string>(\"getPackageName\");\n            var packageInfo = currentActivity.Call<AndroidJavaObject>(\"getPackageManager\").Call<AndroidJavaObject>(\"getPackageInfo\", m_ObbPackage, 0);\n            m_ObbVersion = packageInfo.Get<int>(\"versionCode\");\n        }\n    }\n}\n"
  },
  {
    "path": "CHANGELOG",
    "content": "Version 3.0 (2017-09-22)\n\n* Scripts rewritten, with IGooglePlayObbDownloader interface extracted\n* Sample updated to match the new API\n\n\nVersion 2.0 (2017-08-25)\n\n* Updated the plugin to be an AAR\n* Solved Android M compatibility issues (however min API level is now 11)\n* Updated project structure and documentation\n\n\nVersion 1.0\n\n* Initial version"
  },
  {
    "path": "LICENSE.txt",
    "content": "\n                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   Copyright 2015-2016 Yury Habets\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n"
  },
  {
    "path": "README.md",
    "content": "# UnityOBBDownloader\nThis package is an adaption of the Google Play market_downloader library, for use with Unity Android (as a plugin).\n\nThis plugin does NOT solve splitting up a >100MB .apk into .obb (through asset bundles or similar techniques).\nIt merely handles the downloading of .obb files attached to a published .apk, on devices that don't support automatic downloading.\n\nThis software is free and published as is, with no warranty and responsibilities - use it at your own risk.\n\n## Usage\nThis plugin is published as an AAR, and is compatible with Unity 5+. Minimum Android version supported is Honeycomb (API level 11). For a version compatible with Unity 4 or with older Android devices, please checkout changeset tagged `1.0`.\nThe C# API of the plugin is available in `IGooglePlayObbDownloader` interface. Use `GooglePlayObbDownloadManager.GetGooglePlayObbDownloader()` to obtain an instance.\n\n0.\tAdd the plugin to your project. You need the AAR and the C# scripts (`Assets/Plugins/Android/unityOBBDownloader.aar`, `Assets/Scripts/GooglePlayDownloader.cs` and `Assets/Scripts/GooglePlayDownloaderImpl.cs`)\n1.\tIn your script, don't forget to set `PublicKey` to your own value. You'll see a message logged and probably a crash if you skip this step\n2.\tChange the Bundle Identifier / Version Code so it matches the application already available on Google Play (that has .obb files attached)\n3.\tBuild and Run on your Android device\n\nFor a script sample, please refer to `Assets/Scripts/DownloadObbExample.cs`.\n\n## How to Build\nRun `gradlew assemble` from src/UnityAndroidPermissions/\n\n## License\nCopyright (C) 2016-2017 Yury Habets\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\n## See also\nFor more information on using .obb files in Unity, please refer to http://docs.unity3d.com/Manual/android-OBBsupport.html and http://developer.android.com/guide/market/expansion-files.html\n\nFor more information on Unity Android plugins, please refer to http://docs.unity3d.com/Manual/PluginsForAndroid.html\n"
  },
  {
    "path": "src/unityOBBDownloader/build.gradle",
    "content": "apply plugin: 'com.android.library'\n\nbuildscript {\n\trepositories {\n\t\tjcenter()\n\t\tgoogle()\n\t}\n\n\tdependencies {\n\t\tclasspath 'com.android.tools.build:gradle:3.0.1'\n\t}\n} \n\nandroid {\n    compileSdkVersion 26\n    buildToolsVersion \"26.0.2\"\n\n    defaultConfig {\n        minSdkVersion 11\n        targetSdkVersion 26\n    }\n\n    buildTypes {\n        release {\n            minifyEnabled true\n            proguardFiles 'proguard.cfg'\n        }\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/gradle/wrapper/gradle-wrapper.properties",
    "content": "distributionBase=GRADLE_USER_HOME\ndistributionPath=wrapper/dists\nzipStoreBase=GRADLE_USER_HOME\nzipStorePath=wrapper/dists\ndistributionUrl=https\\://services.gradle.org/distributions/gradle-4.2.1-bin.zip\n"
  },
  {
    "path": "src/unityOBBDownloader/gradlew",
    "content": "#!/usr/bin/env sh\n\n##############################################################################\n##\n##  Gradle start up script for UN*X\n##\n##############################################################################\n\n# Attempt to set APP_HOME\n# Resolve links: $0 may be a link\nPRG=\"$0\"\n# Need this for relative symlinks.\nwhile [ -h \"$PRG\" ] ; do\n    ls=`ls -ld \"$PRG\"`\n    link=`expr \"$ls\" : '.*-> \\(.*\\)$'`\n    if expr \"$link\" : '/.*' > /dev/null; then\n        PRG=\"$link\"\n    else\n        PRG=`dirname \"$PRG\"`\"/$link\"\n    fi\ndone\nSAVED=\"`pwd`\"\ncd \"`dirname \\\"$PRG\\\"`/\" >/dev/null\nAPP_HOME=\"`pwd -P`\"\ncd \"$SAVED\" >/dev/null\n\nAPP_NAME=\"Gradle\"\nAPP_BASE_NAME=`basename \"$0\"`\n\n# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.\nDEFAULT_JVM_OPTS=\"\"\n\n# Use the maximum available, or set MAX_FD != -1 to use that value.\nMAX_FD=\"maximum\"\n\nwarn () {\n    echo \"$*\"\n}\n\ndie () {\n    echo\n    echo \"$*\"\n    echo\n    exit 1\n}\n\n# OS specific support (must be 'true' or 'false').\ncygwin=false\nmsys=false\ndarwin=false\nnonstop=false\ncase \"`uname`\" in\n  CYGWIN* )\n    cygwin=true\n    ;;\n  Darwin* )\n    darwin=true\n    ;;\n  MINGW* )\n    msys=true\n    ;;\n  NONSTOP* )\n    nonstop=true\n    ;;\nesac\n\nCLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar\n\n# Determine the Java command to use to start the JVM.\nif [ -n \"$JAVA_HOME\" ] ; then\n    if [ -x \"$JAVA_HOME/jre/sh/java\" ] ; then\n        # IBM's JDK on AIX uses strange locations for the executables\n        JAVACMD=\"$JAVA_HOME/jre/sh/java\"\n    else\n        JAVACMD=\"$JAVA_HOME/bin/java\"\n    fi\n    if [ ! -x \"$JAVACMD\" ] ; then\n        die \"ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME\n\nPlease set the JAVA_HOME variable in your environment to match the\nlocation of your Java installation.\"\n    fi\nelse\n    JAVACMD=\"java\"\n    which java >/dev/null 2>&1 || die \"ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.\n\nPlease set the JAVA_HOME variable in your environment to match the\nlocation of your Java installation.\"\nfi\n\n# Increase the maximum file descriptors if we can.\nif [ \"$cygwin\" = \"false\" -a \"$darwin\" = \"false\" -a \"$nonstop\" = \"false\" ] ; then\n    MAX_FD_LIMIT=`ulimit -H -n`\n    if [ $? -eq 0 ] ; then\n        if [ \"$MAX_FD\" = \"maximum\" -o \"$MAX_FD\" = \"max\" ] ; then\n            MAX_FD=\"$MAX_FD_LIMIT\"\n        fi\n        ulimit -n $MAX_FD\n        if [ $? -ne 0 ] ; then\n            warn \"Could not set maximum file descriptor limit: $MAX_FD\"\n        fi\n    else\n        warn \"Could not query maximum file descriptor limit: $MAX_FD_LIMIT\"\n    fi\nfi\n\n# For Darwin, add options to specify how the application appears in the dock\nif $darwin; then\n    GRADLE_OPTS=\"$GRADLE_OPTS \\\"-Xdock:name=$APP_NAME\\\" \\\"-Xdock:icon=$APP_HOME/media/gradle.icns\\\"\"\nfi\n\n# For Cygwin, switch paths to Windows format before running java\nif $cygwin ; then\n    APP_HOME=`cygpath --path --mixed \"$APP_HOME\"`\n    CLASSPATH=`cygpath --path --mixed \"$CLASSPATH\"`\n    JAVACMD=`cygpath --unix \"$JAVACMD\"`\n\n    # We build the pattern for arguments to be converted via cygpath\n    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`\n    SEP=\"\"\n    for dir in $ROOTDIRSRAW ; do\n        ROOTDIRS=\"$ROOTDIRS$SEP$dir\"\n        SEP=\"|\"\n    done\n    OURCYGPATTERN=\"(^($ROOTDIRS))\"\n    # Add a user-defined pattern to the cygpath arguments\n    if [ \"$GRADLE_CYGPATTERN\" != \"\" ] ; then\n        OURCYGPATTERN=\"$OURCYGPATTERN|($GRADLE_CYGPATTERN)\"\n    fi\n    # Now convert the arguments - kludge to limit ourselves to /bin/sh\n    i=0\n    for arg in \"$@\" ; do\n        CHECK=`echo \"$arg\"|egrep -c \"$OURCYGPATTERN\" -`\n        CHECK2=`echo \"$arg\"|egrep -c \"^-\"`                                 ### Determine if an option\n\n        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### Added a condition\n            eval `echo args$i`=`cygpath --path --ignore --mixed \"$arg\"`\n        else\n            eval `echo args$i`=\"\\\"$arg\\\"\"\n        fi\n        i=$((i+1))\n    done\n    case $i in\n        (0) set -- ;;\n        (1) set -- \"$args0\" ;;\n        (2) set -- \"$args0\" \"$args1\" ;;\n        (3) set -- \"$args0\" \"$args1\" \"$args2\" ;;\n        (4) set -- \"$args0\" \"$args1\" \"$args2\" \"$args3\" ;;\n        (5) set -- \"$args0\" \"$args1\" \"$args2\" \"$args3\" \"$args4\" ;;\n        (6) set -- \"$args0\" \"$args1\" \"$args2\" \"$args3\" \"$args4\" \"$args5\" ;;\n        (7) set -- \"$args0\" \"$args1\" \"$args2\" \"$args3\" \"$args4\" \"$args5\" \"$args6\" ;;\n        (8) set -- \"$args0\" \"$args1\" \"$args2\" \"$args3\" \"$args4\" \"$args5\" \"$args6\" \"$args7\" ;;\n        (9) set -- \"$args0\" \"$args1\" \"$args2\" \"$args3\" \"$args4\" \"$args5\" \"$args6\" \"$args7\" \"$args8\" ;;\n    esac\nfi\n\n# Escape application args\nsave () {\n    for i do printf %s\\\\n \"$i\" | sed \"s/'/'\\\\\\\\''/g;1s/^/'/;\\$s/\\$/' \\\\\\\\/\" ; done\n    echo \" \"\n}\nAPP_ARGS=$(save \"$@\")\n\n# Collect all arguments for the java command, following the shell quoting and substitution rules\neval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS \"\\\"-Dorg.gradle.appname=$APP_BASE_NAME\\\"\" -classpath \"\\\"$CLASSPATH\\\"\" org.gradle.wrapper.GradleWrapperMain \"$APP_ARGS\"\n\n# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong\nif [ \"$(uname)\" = \"Darwin\" ] && [ \"$HOME\" = \"$PWD\" ]; then\n  cd \"$(dirname \"$0\")\"\nfi\n\nexec \"$JAVACMD\" \"$@\"\n"
  },
  {
    "path": "src/unityOBBDownloader/gradlew.bat",
    "content": "@if \"%DEBUG%\" == \"\" @echo off\n@rem ##########################################################################\n@rem\n@rem  Gradle startup script for Windows\n@rem\n@rem ##########################################################################\n\n@rem Set local scope for the variables with windows NT shell\nif \"%OS%\"==\"Windows_NT\" setlocal\n\nset DIRNAME=%~dp0\nif \"%DIRNAME%\" == \"\" set DIRNAME=.\nset APP_BASE_NAME=%~n0\nset APP_HOME=%DIRNAME%\n\n@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.\nset DEFAULT_JVM_OPTS=\n\n@rem Find java.exe\nif defined JAVA_HOME goto findJavaFromJavaHome\n\nset JAVA_EXE=java.exe\n%JAVA_EXE% -version >NUL 2>&1\nif \"%ERRORLEVEL%\" == \"0\" goto init\n\necho.\necho ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.\necho.\necho Please set the JAVA_HOME variable in your environment to match the\necho location of your Java installation.\n\ngoto fail\n\n:findJavaFromJavaHome\nset JAVA_HOME=%JAVA_HOME:\"=%\nset JAVA_EXE=%JAVA_HOME%/bin/java.exe\n\nif exist \"%JAVA_EXE%\" goto init\n\necho.\necho ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%\necho.\necho Please set the JAVA_HOME variable in your environment to match the\necho location of your Java installation.\n\ngoto fail\n\n:init\n@rem Get command-line arguments, handling Windows variants\n\nif not \"%OS%\" == \"Windows_NT\" goto win9xME_args\n\n:win9xME_args\n@rem Slurp the command line arguments.\nset CMD_LINE_ARGS=\nset _SKIP=2\n\n:win9xME_args_slurp\nif \"x%~1\" == \"x\" goto execute\n\nset CMD_LINE_ARGS=%*\n\n:execute\n@rem Setup the command line\n\nset CLASSPATH=%APP_HOME%\\gradle\\wrapper\\gradle-wrapper.jar\n\n@rem Execute Gradle\n\"%JAVA_EXE%\" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% \"-Dorg.gradle.appname=%APP_BASE_NAME%\" -classpath \"%CLASSPATH%\" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%\n\n:end\n@rem End local scope for the variables with windows NT shell\nif \"%ERRORLEVEL%\"==\"0\" goto mainEnd\n\n:fail\nrem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of\nrem the _cmd.exe /c_ return code!\nif  not \"\" == \"%GRADLE_EXIT_CONSOLE%\" exit 1\nexit /b 1\n\n:mainEnd\nif \"%OS%\"==\"Windows_NT\" endlocal\n\n:omega\n"
  },
  {
    "path": "src/unityOBBDownloader/proguard.cfg",
    "content": "-optimizationpasses 5\n-dontusemixedcaseclassnames\n-dontskipnonpubliclibraryclasses\n-dontpreverify\n-verbose\n-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*\n\n-keep public class * extends android.app.Activity\n-keep public class * extends android.app.Application\n-keep public class * extends com.google.android.vending.expansion.downloader.impl.DownloaderService\n-keep public class * extends android.content.BroadcastReceiver\n-keep public class * extends android.content.ContentProvider\n-keep public class * extends android.app.backup.BackupAgentHelper\n-keep public class * extends android.preference.Preference\n\n-keepclasseswithmembernames class * {\n    native <methods>;\n}\n\n-keepclasseswithmembers class * {\n    public <init>(android.content.Context, android.util.AttributeSet);\n}\n\n-keepclasseswithmembers class * {\n    public <init>(android.content.Context, android.util.AttributeSet, int);\n}\n\n-keepclassmembers class * extends android.app.Activity {\n   public void *(android.view.View);\n}\n\n-keepclassmembers enum * {\n    public static **[] values();\n    public static ** valueOf(java.lang.String);\n}\n\n-keepclassmembers class * implements android.os.Parcelable {\n  public static final android.os.Parcelable$Creator *;\n}\n\n# remove obfuscation on critical parts of the google play downloader\n-keepclassmembers class * extends android.app.Service { <fields>; }\n-flattenpackagehierarchy com.unity3d.plugin.downloader\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/AndroidManifest.xml",
    "content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<manifest\n    xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    package=\"com.unity3d.plugin.downloader\"\n    android:versionCode=\"1\"\n    android:versionName=\"1.0\">\n    <application>\n        <activity android:name=\"com.unity3d.plugin.downloader.UnityDownloaderActivity\" />\n        <service android:name=\"com.unity3d.plugin.downloader.UnityDownloaderService\" />\n        <receiver android:name=\"com.unity3d.plugin.downloader.UnityAlarmReceiver\" />\n    </application>\n    <uses-sdk android:minSdkVersion=\"11\"/>\n    <uses-permission android:name=\"com.android.vending.CHECK_LICENSE\" />\n    <uses-permission android:name=\"android.permission.INTERNET\" />\n    <uses-permission android:name=\"android.permission.WAKE_LOCK\" />\n    <uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" />\n    <uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>\n    <uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" />\n</manifest>\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/aidl/com/android/vending/licensing/ILicenseResultListener.aidl",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.android.vending.licensing;\n\noneway interface ILicenseResultListener {\n  void verifyLicense(int responseCode, String signedData, String signature);\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/aidl/com/android/vending/licensing/ILicensingService.aidl",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.android.vending.licensing;\n\nimport com.android.vending.licensing.ILicenseResultListener;\n\noneway interface ILicensingService {\n  void checkLicense(long nonce, String packageName, in ILicenseResultListener listener);\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/Constants.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader;\n\nimport java.io.File;\n\n\n/**\n * Contains the internal constants that are used in the download manager.\n * As a general rule, modifying these constants should be done with care.\n */\npublic class Constants {    \n    /** Tag used for debugging/logging */\n    public static final String TAG = \"LVLDL\";\n\n    /**\n     * Expansion path where we store obb files\n     */\n    public static final String EXP_PATH = File.separator + \"Android\"\n            + File.separator + \"obb\" + File.separator;\n    \n    /** The intent that gets sent when the service must wake up for a retry */\n    public static final String ACTION_RETRY = \"android.intent.action.DOWNLOAD_WAKEUP\";\n\n    /** the intent that gets sent when clicking a successful download */\n    public static final String ACTION_OPEN = \"android.intent.action.DOWNLOAD_OPEN\";\n\n    /** the intent that gets sent when clicking an incomplete/failed download  */\n    public static final String ACTION_LIST = \"android.intent.action.DOWNLOAD_LIST\";\n\n    /** the intent that gets sent when deleting the notification of a completed download */\n    public static final String ACTION_HIDE = \"android.intent.action.DOWNLOAD_HIDE\";\n\n    /**\n     * When a number has to be appended to the filename, this string is used to separate the\n     * base filename from the sequence number\n     */\n    public static final String FILENAME_SEQUENCE_SEPARATOR = \"-\";\n\n    /** The default user agent used for downloads */\n    public static final String DEFAULT_USER_AGENT = \"Android.LVLDM\";\n\n    /** The buffer size used to stream the data */\n    public static final int BUFFER_SIZE = 4096;\n\n    /** The minimum amount of progress that has to be done before the progress bar gets updated */\n    public static final int MIN_PROGRESS_STEP = 4096;\n\n    /** The minimum amount of time that has to elapse before the progress bar gets updated, in ms */\n    public static final long MIN_PROGRESS_TIME = 1000;\n\n    /** The maximum number of rows in the database (FIFO) */\n    public static final int MAX_DOWNLOADS = 1000;\n\n    /**\n     * The number of times that the download manager will retry its network\n     * operations when no progress is happening before it gives up.\n     */\n    public static final int MAX_RETRIES = 5;\n\n    /**\n     * The minimum amount of time that the download manager accepts for\n     * a Retry-After response header with a parameter in delta-seconds.\n     */\n    public static final int MIN_RETRY_AFTER = 30; // 30s\n\n    /**\n     * The maximum amount of time that the download manager accepts for\n     * a Retry-After response header with a parameter in delta-seconds.\n     */\n    public static final int MAX_RETRY_AFTER = 24 * 60 * 60; // 24h\n\n    /**\n     * The maximum number of redirects.\n     */\n    public static final int MAX_REDIRECTS = 5; // can't be more than 7.\n\n    /**\n     * The time between a failure and the first retry after an IOException.\n     * Each subsequent retry grows exponentially, doubling each time.\n     * The time is in seconds.\n     */\n    public static final int RETRY_FIRST_DELAY = 30;\n\n    /** Enable separate connectivity logging */\n    public static final boolean LOGX = true;\n\n    /** Enable verbose logging */\n    public static final boolean LOGV = false;\n    \n    /** Enable super-verbose logging */\n    private static final boolean LOCAL_LOGVV = false;\n    public static final boolean LOGVV = LOCAL_LOGVV && LOGV;\n    \n    /**\n     * This download has successfully completed.\n     * Warning: there might be other status values that indicate success\n     * in the future.\n     * Use isSucccess() to capture the entire category.\n     */\n    public static final int STATUS_SUCCESS = 200;\n\n    /**\n     * This request couldn't be parsed. This is also used when processing\n     * requests with unknown/unsupported URI schemes.\n     */\n    public static final int STATUS_BAD_REQUEST = 400;\n\n    /**\n     * This download can't be performed because the content type cannot be\n     * handled.\n     */\n    public static final int STATUS_NOT_ACCEPTABLE = 406;\n\n    /**\n     * This download cannot be performed because the length cannot be\n     * determined accurately. This is the code for the HTTP error \"Length\n     * Required\", which is typically used when making requests that require\n     * a content length but don't have one, and it is also used in the\n     * client when a response is received whose length cannot be determined\n     * accurately (therefore making it impossible to know when a download\n     * completes).\n     */\n    public static final int STATUS_LENGTH_REQUIRED = 411;\n\n    /**\n     * This download was interrupted and cannot be resumed.\n     * This is the code for the HTTP error \"Precondition Failed\", and it is\n     * also used in situations where the client doesn't have an ETag at all.\n     */\n    public static final int STATUS_PRECONDITION_FAILED = 412;\n\n    /**\n     * The lowest-valued error status that is not an actual HTTP status code.\n     */\n    public static final int MIN_ARTIFICIAL_ERROR_STATUS = 488;\n\n    /**\n     * The requested destination file already exists.\n     */\n    public static final int STATUS_FILE_ALREADY_EXISTS_ERROR = 488;\n\n    /**\n     * Some possibly transient error occurred, but we can't resume the download.\n     */\n    public static final int STATUS_CANNOT_RESUME = 489;\n\n    /**\n     * This download was canceled\n     */\n    public static final int STATUS_CANCELED = 490;\n\n    /**\n     * This download has completed with an error.\n     * Warning: there will be other status values that indicate errors in\n     * the future. Use isStatusError() to capture the entire category.\n     */\n    public static final int STATUS_UNKNOWN_ERROR = 491;\n\n    /**\n     * This download couldn't be completed because of a storage issue.\n     * Typically, that's because the filesystem is missing or full.\n     * Use the more specific {@link #STATUS_INSUFFICIENT_SPACE_ERROR}\n     * and {@link #STATUS_DEVICE_NOT_FOUND_ERROR} when appropriate.\n     */\n    public static final int STATUS_FILE_ERROR = 492;\n\n    /**\n     * This download couldn't be completed because of an HTTP\n     * redirect response that the download manager couldn't\n     * handle.\n     */\n    public static final int STATUS_UNHANDLED_REDIRECT = 493;\n\n    /**\n     * This download couldn't be completed because of an\n     * unspecified unhandled HTTP code.\n     */\n    public static final int STATUS_UNHANDLED_HTTP_CODE = 494;\n\n    /**\n     * This download couldn't be completed because of an\n     * error receiving or processing data at the HTTP level.\n     */\n    public static final int STATUS_HTTP_DATA_ERROR = 495;\n\n    /**\n     * This download couldn't be completed because of an\n     * HttpException while setting up the request.\n     */\n    public static final int STATUS_HTTP_EXCEPTION = 496;\n\n    /**\n     * This download couldn't be completed because there were\n     * too many redirects.\n     */\n    public static final int STATUS_TOO_MANY_REDIRECTS = 497;\n\n    /**\n     * This download couldn't be completed due to insufficient storage\n     * space.  Typically, this is because the SD card is full.\n     */\n    public static final int STATUS_INSUFFICIENT_SPACE_ERROR = 498;\n\n    /**\n     * This download couldn't be completed because no external storage\n     * device was found.  Typically, this is because the SD card is not\n     * mounted.\n     */\n    public static final int STATUS_DEVICE_NOT_FOUND_ERROR = 499;\n\n    /**\n     * The wake duration to check to see if a download is possible.\n     */\n    public static final long WATCHDOG_WAKE_TIMER = 60*1000;    \n\n    /**\n     * The wake duration to check to see if the process was killed.\n     */\n    public static final long ACTIVE_THREAD_WATCHDOG = 5*1000;    \n\n}"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/DownloadProgressInfo.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader;\n\nimport android.os.Parcel;\nimport android.os.Parcelable;\n\n\n/**\n * This class contains progress information about the active download(s).\n *\n * When you build the Activity that initiates a download and tracks the\n * progress by implementing the {@link IDownloaderClient} interface, you'll\n * receive a DownloadProgressInfo object in each call to the {@link\n * IDownloaderClient#onDownloadProgress} method. This allows you to update\n * your activity's UI with information about the download progress, such\n * as the progress so far, time remaining and current speed.\n */\npublic class DownloadProgressInfo implements Parcelable {\n    public long mOverallTotal;\n    public long mOverallProgress;\n    public long mTimeRemaining; // time remaining\n    public float mCurrentSpeed; // speed in KB/S\n\n    @Override\n    public int describeContents() {\n        return 0;\n    }\n\n    @Override\n    public void writeToParcel(Parcel p, int i) {\n        p.writeLong(mOverallTotal);\n        p.writeLong(mOverallProgress);\n        p.writeLong(mTimeRemaining);\n        p.writeFloat(mCurrentSpeed);\n    }\n\n    public DownloadProgressInfo(Parcel p) {\n        mOverallTotal = p.readLong();\n        mOverallProgress = p.readLong();\n        mTimeRemaining = p.readLong();\n        mCurrentSpeed = p.readFloat();\n    }\n\n    public DownloadProgressInfo(long overallTotal, long overallProgress,\n            long timeRemaining,\n            float currentSpeed) {\n        this.mOverallTotal = overallTotal;\n        this.mOverallProgress = overallProgress;\n        this.mTimeRemaining = timeRemaining;\n        this.mCurrentSpeed = currentSpeed;\n    }\n\n    public static final Creator<DownloadProgressInfo> CREATOR = new Creator<DownloadProgressInfo>() {\n        @Override\n        public DownloadProgressInfo createFromParcel(Parcel parcel) {\n            return new DownloadProgressInfo(parcel);\n        }\n\n        @Override\n        public DownloadProgressInfo[] newArray(int i) {\n            return new DownloadProgressInfo[i];\n        }\n    };\n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/DownloaderClientMarshaller.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader;\n\nimport com.google.android.vending.expansion.downloader.impl.DownloaderService;\n\nimport android.app.PendingIntent;\nimport android.content.ComponentName;\nimport android.content.Context;\nimport android.content.Intent;\nimport android.content.ServiceConnection;\nimport android.content.pm.PackageManager.NameNotFoundException;\nimport android.os.Bundle;\nimport android.os.Handler;\nimport android.os.IBinder;\nimport android.os.Message;\nimport android.os.Messenger;\nimport android.os.RemoteException;\nimport android.util.Log;\n\n\n\n/**\n * This class binds the service API to your application client.  It contains the IDownloaderClient proxy,\n * which is used to call functions in your client as well as the Stub, which is used to call functions\n * in the client implementation of IDownloaderClient.\n * \n * <p>The IPC is implemented using an Android Messenger and a service Binder.  The connect method\n * should be called whenever the client wants to bind to the service.  It opens up a service connection\n * that ends up calling the onServiceConnected client API that passes the service messenger\n * in.  If the client wants to be notified by the service, it is responsible for then passing its\n * messenger to the service in a separate call.\n *\n * <p>Critical methods are {@link #startDownloadServiceIfRequired} and {@link #CreateStub}.\n *\n * <p>When your application first starts, you should first check whether your app's expansion files are\n * already on the device. If not, you should then call {@link #startDownloadServiceIfRequired}, which\n * starts your {@link impl.DownloaderService} to download the expansion files if necessary. The method\n * returns a value indicating whether download is required or not.\n *\n * <p>If a download is required, {@link #startDownloadServiceIfRequired} begins the download through\n * the specified service and you should then call {@link #CreateStub} to instantiate a member {@link\n * IStub} object that you need in order to receive calls through your {@link IDownloaderClient}\n * interface.\n */\npublic class DownloaderClientMarshaller {\n    public static final int MSG_ONDOWNLOADSTATE_CHANGED = 10;\n    public static final int MSG_ONDOWNLOADPROGRESS = 11;\n    public static final int MSG_ONSERVICECONNECTED = 12;\n\n    public static final String PARAM_NEW_STATE = \"newState\";\n    public static final String PARAM_PROGRESS = \"progress\";\n    public static final String PARAM_MESSENGER = DownloaderService.EXTRA_MESSAGE_HANDLER;\n\n    public static final int NO_DOWNLOAD_REQUIRED = DownloaderService.NO_DOWNLOAD_REQUIRED;\n    public static final int LVL_CHECK_REQUIRED = DownloaderService.LVL_CHECK_REQUIRED;\n    public static final int DOWNLOAD_REQUIRED = DownloaderService.DOWNLOAD_REQUIRED;\n\n    private static class Proxy implements IDownloaderClient {\n        private Messenger mServiceMessenger;\n\n        @Override\n        public void onDownloadStateChanged(int newState) {\n            Bundle params = new Bundle(1);\n            params.putInt(PARAM_NEW_STATE, newState);\n            send(MSG_ONDOWNLOADSTATE_CHANGED, params);\n        }\n\n        @Override\n        public void onDownloadProgress(DownloadProgressInfo progress) {\n            Bundle params = new Bundle(1);\n            params.putParcelable(PARAM_PROGRESS, progress);\n            send(MSG_ONDOWNLOADPROGRESS, params);\n        }\n\n        private void send(int method, Bundle params) {\n            Message m = Message.obtain(null, method);\n            m.setData(params);\n            try {\n                mServiceMessenger.send(m);\n            } catch (RemoteException e) {\n                e.printStackTrace();\n            }\n        }\n        \n        public Proxy(Messenger msg) {\n            mServiceMessenger = msg;\n        }\n\n        @Override\n        public void onServiceConnected(Messenger m) {\n            /**\n             * This is never called through the proxy.\n             */\n        }\n    }\n\n    private static class Stub implements IStub {\n        private IDownloaderClient mItf = null;\n        private Class<?> mDownloaderServiceClass;\n        private boolean mBound;\n        private Messenger mServiceMessenger;\n        private Context mContext;\n        /**\n         * Target we publish for clients to send messages to IncomingHandler.\n         */\n        final Messenger mMessenger = new Messenger(new Handler() {\n            @Override\n            public void handleMessage(Message msg) {\n                switch (msg.what) {\n                    case MSG_ONDOWNLOADPROGRESS:                        \n                        Bundle bun = msg.getData();\n                        if ( null != mContext ) {\n                            bun.setClassLoader(mContext.getClassLoader());\n                            DownloadProgressInfo dpi = (DownloadProgressInfo) msg.getData()\n                                    .getParcelable(PARAM_PROGRESS);\n                            mItf.onDownloadProgress(dpi);\n                        }\n                        break;\n                    case MSG_ONDOWNLOADSTATE_CHANGED:\n                        mItf.onDownloadStateChanged(msg.getData().getInt(PARAM_NEW_STATE));\n                        break;\n                    case MSG_ONSERVICECONNECTED:\n                        mItf.onServiceConnected(\n                                (Messenger) msg.getData().getParcelable(PARAM_MESSENGER));\n                        break;\n                }\n            }\n        });\n\n        public Stub(IDownloaderClient itf, Class<?> downloaderService) {\n            mItf = itf;\n            mDownloaderServiceClass = downloaderService;\n        }\n\n        /**\n         * Class for interacting with the main interface of the service.\n         */\n        private ServiceConnection mConnection = new ServiceConnection() {\n            public void onServiceConnected(ComponentName className, IBinder service) {\n                // This is called when the connection with the service has been\n                // established, giving us the object we can use to\n                // interact with the service. We are communicating with the\n                // service using a Messenger, so here we get a client-side\n                // representation of that from the raw IBinder object.\n                mServiceMessenger = new Messenger(service);\n                mItf.onServiceConnected(\n                        mServiceMessenger);\n            }\n\n            public void onServiceDisconnected(ComponentName className) {\n                // This is called when the connection with the service has been\n                // unexpectedly disconnected -- that is, its process crashed.\n                mServiceMessenger = null;\n            }\n        };\n\n        @Override\n        public void connect(Context c) {\n            mContext = c;\n            Intent bindIntent = new Intent(c, mDownloaderServiceClass);\n            bindIntent.putExtra(PARAM_MESSENGER, mMessenger);\n            if ( !c.bindService(bindIntent, mConnection, Context.BIND_DEBUG_UNBIND) ) {\n                if ( Constants.LOGVV ) {\n                    Log.d(Constants.TAG, \"Service Unbound\");\n                }\n            } else {\n                mBound = true;\n            }\n                \n        }\n\n        @Override\n        public void disconnect(Context c) {\n            if (mBound) {\n                c.unbindService(mConnection);\n                mBound = false;\n            }\n            mContext = null;\n        }\n\n        @Override\n        public Messenger getMessenger() {\n            return mMessenger;\n        }\n    }\n\n    /**\n     * Returns a proxy that will marshal calls to IDownloaderClient methods\n     * \n     * @param msg\n     * @return\n     */\n    public static IDownloaderClient CreateProxy(Messenger msg) {\n        return new Proxy(msg);\n    }\n\n    /**\n     * Returns a stub object that, when connected, will listen for marshaled\n     * {@link IDownloaderClient} methods and translate them into calls to the supplied\n     * interface.\n     * \n     * @param itf An implementation of IDownloaderClient that will be called\n     *            when remote method calls are unmarshaled.\n     * @param downloaderService The class for your implementation of {@link\n     * impl.DownloaderService}.\n     * @return The {@link IStub} that allows you to connect to the service such that\n     * your {@link IDownloaderClient} receives status updates.\n     */\n    public static IStub CreateStub(IDownloaderClient itf, Class<?> downloaderService) {\n        return new Stub(itf, downloaderService);\n    }\n    \n    /**\n     * Starts the download if necessary. This function starts a flow that does `\n     * many things. 1) Checks to see if the APK version has been checked and\n     * the metadata database updated 2) If the APK version does not match,\n     * checks the new LVL status to see if a new download is required 3) If the\n     * APK version does match, then checks to see if the download(s) have been\n     * completed 4) If the downloads have been completed, returns\n     * NO_DOWNLOAD_REQUIRED The idea is that this can be called during the\n     * startup of an application to quickly ascertain if the application needs\n     * to wait to hear about any updated APK expansion files. Note that this does\n     * mean that the application MUST be run for the first time with a network\n     * connection, even if Market delivers all of the files.\n     * \n     * @param context Your application Context.\n     * @param notificationClient A PendingIntent to start the Activity in your application\n     * that shows the download progress and which will also start the application when download\n     * completes.\n     * @param serviceClass the class of your {@link imp.DownloaderService} implementation\n     * @return whether the service was started and the reason for starting the service.\n     * Either {@link #NO_DOWNLOAD_REQUIRED}, {@link #LVL_CHECK_REQUIRED}, or {@link\n     * #DOWNLOAD_REQUIRED}.\n     * @throws NameNotFoundException\n     */\n    public static int startDownloadServiceIfRequired(Context context, PendingIntent notificationClient, \n            Class<?> serviceClass)\n            throws NameNotFoundException {\n        return DownloaderService.startDownloadServiceIfRequired(context, notificationClient,\n                serviceClass);\n    }\n    \n    /**\n     * This version assumes that the intent contains the pending intent as a parameter. This\n     * is used for responding to alarms.\n     * <p>The pending intent must be in an extra with the key {@link \n     * impl.DownloaderService#EXTRA_PENDING_INTENT}.\n     * \n     * @param context\n     * @param notificationClient\n     * @param serviceClass the class of the service to start\n     * @return\n     * @throws NameNotFoundException\n     */\n    public static int startDownloadServiceIfRequired(Context context, Intent notificationClient, \n            Class<?> serviceClass)\n            throws NameNotFoundException {\n        return DownloaderService.startDownloadServiceIfRequired(context, notificationClient,\n                serviceClass);\n    }    \n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/DownloaderServiceMarshaller.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader;\n\nimport com.google.android.vending.expansion.downloader.impl.DownloaderService;\n\nimport android.content.Context;\nimport android.os.Bundle;\nimport android.os.Handler;\nimport android.os.Message;\nimport android.os.Messenger;\nimport android.os.RemoteException;\n\n\n\n/**\n * This class is used by the client activity to proxy requests to the Downloader\n * Service.\n *\n * Most importantly, you must call {@link #CreateProxy} during the {@link\n * IDownloaderClient#onServiceConnected} callback in your activity in order to instantiate\n * an {@link IDownloaderService} object that you can then use to issue commands to the {@link\n * DownloaderService} (such as to pause and resume downloads).\n */\npublic class DownloaderServiceMarshaller {\n\n    public static final int MSG_REQUEST_ABORT_DOWNLOAD =\n            1;\n    public static final int MSG_REQUEST_PAUSE_DOWNLOAD =\n            2;\n    public static final int MSG_SET_DOWNLOAD_FLAGS =\n            3;\n    public static final int MSG_REQUEST_CONTINUE_DOWNLOAD =\n            4;\n    public static final int MSG_REQUEST_DOWNLOAD_STATE =\n            5;\n    public static final int MSG_REQUEST_CLIENT_UPDATE =\n            6;\n\n    public static final String PARAMS_FLAGS = \"flags\";\n    public static final String PARAM_MESSENGER = DownloaderService.EXTRA_MESSAGE_HANDLER;\n\n    private static class Proxy implements IDownloaderService {\n        private Messenger mMsg;\n\n        private void send(int method, Bundle params) {\n            Message m = Message.obtain(null, method);\n            m.setData(params);\n            try {\n                mMsg.send(m);\n            } catch (RemoteException e) {\n                e.printStackTrace();\n            }\n        }\n\n        public Proxy(Messenger msg) {\n            mMsg = msg;\n        }\n\n        @Override\n        public void requestAbortDownload() {\n            send(MSG_REQUEST_ABORT_DOWNLOAD, new Bundle());\n        }\n\n        @Override\n        public void requestPauseDownload() {\n            send(MSG_REQUEST_PAUSE_DOWNLOAD, new Bundle());\n        }\n\n        @Override\n        public void setDownloadFlags(int flags) {\n            Bundle params = new Bundle();\n            params.putInt(PARAMS_FLAGS, flags);\n            send(MSG_SET_DOWNLOAD_FLAGS, params);\n        }\n\n        @Override\n        public void requestContinueDownload() {\n            send(MSG_REQUEST_CONTINUE_DOWNLOAD, new Bundle());\n        }\n\n        @Override\n        public void requestDownloadStatus() {\n            send(MSG_REQUEST_DOWNLOAD_STATE, new Bundle());\n        }\n\n        @Override\n        public void onClientUpdated(Messenger clientMessenger) {\n            Bundle bundle = new Bundle(1);\n            bundle.putParcelable(PARAM_MESSENGER, clientMessenger);\n            send(MSG_REQUEST_CLIENT_UPDATE, bundle);\n        }\n    }\n\n    private static class Stub implements IStub {\n        private IDownloaderService mItf = null;\n        final Messenger mMessenger = new Messenger(new Handler() {\n            @Override\n            public void handleMessage(Message msg) {\n                switch (msg.what) {\n                    case MSG_REQUEST_ABORT_DOWNLOAD:\n                        mItf.requestAbortDownload();\n                        break;\n                    case MSG_REQUEST_CONTINUE_DOWNLOAD:\n                        mItf.requestContinueDownload();\n                        break;\n                    case MSG_REQUEST_PAUSE_DOWNLOAD:\n                        mItf.requestPauseDownload();\n                        break;\n                    case MSG_SET_DOWNLOAD_FLAGS:\n                        mItf.setDownloadFlags(msg.getData().getInt(PARAMS_FLAGS));\n                        break;\n                    case MSG_REQUEST_DOWNLOAD_STATE:\n                        mItf.requestDownloadStatus();\n                        break;\n                    case MSG_REQUEST_CLIENT_UPDATE:\n                        mItf.onClientUpdated((Messenger) msg.getData().getParcelable(\n                                PARAM_MESSENGER));\n                        break;\n                }\n            }\n        });\n\n        public Stub(IDownloaderService itf) {\n            mItf = itf;\n        }\n\n        @Override\n        public Messenger getMessenger() {\n            return mMessenger;\n        }\n\n        @Override\n        public void connect(Context c) {\n\n        }\n\n        @Override\n        public void disconnect(Context c) {\n\n        }\n    }\n\n    /**\n     * Returns a proxy that will marshall calls to IDownloaderService methods\n     * \n     * @param ctx\n     * @return\n     */\n    public static IDownloaderService CreateProxy(Messenger msg) {\n        return new Proxy(msg);\n    }\n\n    /**\n     * Returns a stub object that, when connected, will listen for marshalled\n     * IDownloaderService methods and translate them into calls to the supplied\n     * interface.\n     * \n     * @param itf An implementation of IDownloaderService that will be called\n     *            when remote method calls are unmarshalled.\n     * @return\n     */\n    public static IStub CreateStub(IDownloaderService itf) {\n        return new Stub(itf);\n    }\n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/Helpers.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader;\n\nimport android.annotation.TargetApi;\nimport android.content.Context;\nimport android.os.Build;\nimport android.os.Environment;\nimport android.os.StatFs;\nimport android.os.SystemClock;\nimport android.util.Log;\n\n//import com.android.vending.expansion.downloader.R;\n\nimport java.io.File;\nimport java.text.SimpleDateFormat;\nimport java.util.Date;\nimport java.util.Locale;\nimport java.util.Random;\nimport java.util.TimeZone;\nimport java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\n/**\n * Some helper functions for the download manager\n */\npublic class Helpers {\n\n    public static Random sRandom = new Random(SystemClock.uptimeMillis());\n\n    /** Regex used to parse content-disposition headers */\n    private static final Pattern CONTENT_DISPOSITION_PATTERN = Pattern\n            .compile(\"attachment;\\\\s*filename\\\\s*=\\\\s*\\\"([^\\\"]*)\\\"\");\n\n    private Helpers() {\n    }\n\n    /*\n     * Parse the Content-Disposition HTTP Header. The format of the header is defined here:\n     * http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html This header provides a filename for\n     * content that is going to be downloaded to the file system. We only support the attachment\n     * type.\n     */\n    static String parseContentDisposition(String contentDisposition) {\n        try {\n            Matcher m = CONTENT_DISPOSITION_PATTERN.matcher(contentDisposition);\n            if (m.find()) {\n                return m.group(1);\n            }\n        } catch (IllegalStateException ex) {\n            // This function is defined as returning null when it can't parse\n            // the header\n        }\n        return null;\n    }\n\n    /**\n     * @return the root of the filesystem containing the given path\n     */\n    public static File getFilesystemRoot(String path) {\n        File cache = Environment.getDownloadCacheDirectory();\n        if (path.startsWith(cache.getPath())) {\n            return cache;\n        }\n        File external = Environment.getExternalStorageDirectory();\n        if (path.startsWith(external.getPath())) {\n            return external;\n        }\n        throw new IllegalArgumentException(\n                \"Cannot determine filesystem root for \" + path);\n    }\n\n    public static boolean isExternalMediaMounted() {\n        if (!Environment.getExternalStorageState().equals(\n                Environment.MEDIA_MOUNTED)) {\n            // No SD card found.\n            if (Constants.LOGVV) {\n                Log.d(Constants.TAG, \"no external storage\");\n            }\n            return false;\n        }\n        return true;\n    }\n\n    /**\n     * @return the number of bytes available on the filesystem rooted at the given File\n     */\n    public static long getAvailableBytes(File root) {\n        StatFs stat = new StatFs(root.getPath());\n        // put a bit of margin (in case creating the file grows the system by a\n        // few blocks)\n        long availableBlocks = (long) stat.getAvailableBlocks() - 4;\n        return stat.getBlockSize() * availableBlocks;\n    }\n\n    /**\n     * Checks whether the filename looks legitimate\n     */\n    public static boolean isFilenameValid(String filename) {\n        filename = filename.replaceFirst(\"/+\", \"/\"); // normalize leading\n                                                     // slashes\n        return filename.startsWith(Environment.getDownloadCacheDirectory().toString())\n                || filename.startsWith(Environment.getExternalStorageDirectory().toString());\n    }\n\n    /*\n     * Delete the given file from device\n     */\n    /* package */static void deleteFile(String path) {\n        try {\n            File file = new File(path);\n            file.delete();\n        } catch (Exception e) {\n            Log.w(Constants.TAG, \"file: '\" + path + \"' couldn't be deleted\", e);\n        }\n    }\n\n    /**\n     * Showing progress in MB here. It would be nice to choose the unit (KB, MB, GB) based on total\n     * file size, but given what we know about the expected ranges of file sizes for APK expansion\n     * files, it's probably not necessary.\n     *\n     * @param overallProgress\n     * @param overallTotal\n     * @return\n     */\n\n    static public String getDownloadProgressString(long overallProgress, long overallTotal) {\n        if (overallTotal == 0) {\n            if (Constants.LOGVV) {\n                Log.e(Constants.TAG, \"Notification called when total is zero\");\n            }\n            return \"\";\n        }\n        return String.format(\"%.2f\",\n                (float) overallProgress / (1024.0f * 1024.0f))\n                + \"MB /\" +\n                String.format(\"%.2f\", (float) overallTotal /\n                        (1024.0f * 1024.0f))\n                + \"MB\";\n    }\n\n    /**\n     * Adds a percentile to getDownloadProgressString.\n     *\n     * @param overallProgress\n     * @param overallTotal\n     * @return\n     */\n    static public String getDownloadProgressStringNotification(long overallProgress,\n            long overallTotal) {\n        if (overallTotal == 0) {\n            if (Constants.LOGVV) {\n                Log.e(Constants.TAG, \"Notification called when total is zero\");\n            }\n            return \"\";\n        }\n        return getDownloadProgressString(overallProgress, overallTotal) + \" (\" +\n                getDownloadProgressPercent(overallProgress, overallTotal) + \")\";\n    }\n\n    public static String getDownloadProgressPercent(long overallProgress, long overallTotal) {\n        if (overallTotal == 0) {\n            if (Constants.LOGVV) {\n                Log.e(Constants.TAG, \"Notification called when total is zero\");\n            }\n            return \"\";\n        }\n        return Long.toString(overallProgress * 100 / overallTotal) + \"%\";\n    }\n\n    public static String getSpeedString(float bytesPerMillisecond) {\n        return String.format(\"%.2f\", bytesPerMillisecond * 1000 / 1024);\n    }\n\n    public static String getTimeRemaining(long durationInMilliseconds) {\n        SimpleDateFormat sdf;\n        if (durationInMilliseconds > 1000 * 60 * 60) {\n            sdf = new SimpleDateFormat(\"HH:mm\", Locale.getDefault());\n        } else {\n            sdf = new SimpleDateFormat(\"mm:ss\", Locale.getDefault());\n        }\n        return sdf.format(new Date(durationInMilliseconds - TimeZone.getDefault().getRawOffset()));\n    }\n\n    /**\n     * Returns the file name (without full path) for an Expansion APK file from the given context.\n     *\n     * @param c the context\n     * @param mainFile true for main file, false for patch file\n     * @param versionCode the version of the file\n     * @return String the file name of the expansion file\n     */\n    public static String getExpansionAPKFileName(Context c, boolean mainFile, int versionCode) {\n        return (mainFile ? \"main.\" : \"patch.\") + versionCode + \".\" + c.getPackageName() + \".obb\";\n    }\n\n    /**\n     * Returns the filename (where the file should be saved) from info about a download\n     */\n    static public String generateSaveFileName(Context c, String fileName) {\n        String path = getSaveFilePath(c)\n                + File.separator + fileName;\n        return path;\n    }\n\n    @TargetApi(Build.VERSION_CODES.HONEYCOMB)\n    static public String getSaveFilePath(Context c) {\n        // This technically existed since Honeycomb, but it is critical\n        // on KitKat and greater versions since it will create the\n        // directory if needed\n        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {\n            return c.getObbDir().toString();\n        } else {\n            File root = Environment.getExternalStorageDirectory();\n            String path = root.toString() + Constants.EXP_PATH + c.getPackageName();\n            return path;\n        }\n    }\n\n    /**\n     * Helper function to ascertain the existence of a file and return true/false appropriately\n     *\n     * @param c the app/activity/service context\n     * @param fileName the name (sans path) of the file to query\n     * @param fileSize the size that the file must match\n     * @param deleteFileOnMismatch if the file sizes do not match, delete the file\n     * @return true if it does exist, false otherwise\n     */\n    static public boolean doesFileExist(Context c, String fileName, long fileSize,\n            boolean deleteFileOnMismatch) {\n        // the file may have been delivered by Play --- let's make sure\n        // it's the size we expect\n        File fileForNewFile = new File(Helpers.generateSaveFileName(c, fileName));\n        if (fileForNewFile.exists()) {\n            if (fileForNewFile.length() == fileSize) {\n                return true;\n            }\n            if (deleteFileOnMismatch) {\n                // delete the file --- we won't be able to resume\n                // because we cannot confirm the integrity of the file\n                fileForNewFile.delete();\n            }\n        }\n        return false;\n    }\n\n    public static final int FS_READABLE = 0;\n    public static final int FS_DOES_NOT_EXIST = 1;\n    public static final int FS_CANNOT_READ = 2;\n\n    /**\n     * Helper function to ascertain whether a file can be read.\n     *\n     * @param c the app/activity/service context\n     * @param fileName the name (sans path) of the file to query\n     * @return true if it does exist, false otherwise\n     */\n    static public int getFileStatus(Context c, String fileName) {\n        // the file may have been delivered by Play --- let's make sure\n        // it's the size we expect\n        File fileForNewFile = new File(Helpers.generateSaveFileName(c, fileName));\n        int returnValue;\n        if (fileForNewFile.exists()) {\n            if (fileForNewFile.canRead()) {\n                returnValue = FS_READABLE;\n            } else {\n                returnValue = FS_CANNOT_READ;\n            }\n        } else {\n            returnValue = FS_DOES_NOT_EXIST;\n        }\n        return returnValue;\n    }\n\n    /**\n     * Helper function to ascertain whether the application has the correct access to the OBB\n     * directory to allow an OBB file to be written.\n     * \n     * @param c the app/activity/service context\n     * @return true if the application can write an OBB file, false otherwise\n     */\n    static public boolean canWriteOBBFile(Context c) {\n        String path = getSaveFilePath(c);\n        File fileForNewFile = new File(path);\n        boolean canWrite;\n        if (fileForNewFile.exists()) {\n            canWrite = fileForNewFile.isDirectory() && fileForNewFile.canWrite();\n        } else {\n            canWrite = fileForNewFile.mkdirs();\n        }\n        return canWrite;\n    }\n\n    /**\n     * Converts download states that are returned by the\n     * {@link IDownloaderClient#onDownloadStateChanged} callback into usable strings. This is useful\n     * if using the state strings built into the library to display user messages.\n     * \n     * @param state One of the STATE_* constants from {@link IDownloaderClient}.\n     * @return string resource ID for the corresponding string.\n     */\n    static public int getDownloaderStringResourceIDFromState(Context ctx, int state) {\n        switch (state) {\n            case IDownloaderClient.STATE_IDLE:\n                return getStringResource(ctx, \"state_idle\");\n            case IDownloaderClient.STATE_FETCHING_URL:\n                return getStringResource(ctx, \"state_fetching_url\");\n            case IDownloaderClient.STATE_CONNECTING:\n                return getStringResource(ctx, \"state_connecting\");\n            case IDownloaderClient.STATE_DOWNLOADING:\n                return getStringResource(ctx, \"state_downloading\");\n            case IDownloaderClient.STATE_COMPLETED:\n                return getStringResource(ctx, \"state_completed\");\n            case IDownloaderClient.STATE_PAUSED_NETWORK_UNAVAILABLE:\n                return getStringResource(ctx, \"state_paused_network_unavailable\");\n            case IDownloaderClient.STATE_PAUSED_BY_REQUEST:\n                return getStringResource(ctx, \"state_paused_by_request\");\n            case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION:\n                return getStringResource(ctx, \"state_paused_wifi_disabled\");\n            case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION:\n                return getStringResource(ctx, \"state_paused_wifi_unavailable\");\n            case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED:\n                return getStringResource(ctx, \"state_paused_wifi_disabled\");\n            case IDownloaderClient.STATE_PAUSED_NEED_WIFI:\n                return getStringResource(ctx, \"state_paused_wifi_unavailable\");\n            case IDownloaderClient.STATE_PAUSED_ROAMING:\n                return getStringResource(ctx, \"state_paused_roaming\");\n            case IDownloaderClient.STATE_PAUSED_NETWORK_SETUP_FAILURE:\n                return getStringResource(ctx, \"state_paused_network_setup_failure\");\n            case IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE:\n                return getStringResource(ctx, \"state_paused_sdcard_unavailable\");\n            case IDownloaderClient.STATE_FAILED_UNLICENSED:\n                return getStringResource(ctx, \"state_failed_unlicensed\");\n            case IDownloaderClient.STATE_FAILED_FETCHING_URL:\n                return getStringResource(ctx, \"state_failed_fetching_url\");\n            case IDownloaderClient.STATE_FAILED_SDCARD_FULL:\n                return getStringResource(ctx, \"state_failed_sdcard_full\");\n            case IDownloaderClient.STATE_FAILED_CANCELED:\n                return getStringResource(ctx, \"state_failed_cancelled\");\n            default:\n                return getStringResource(ctx, \"state_unknown\");\n        }\n    }\n\n\tstatic public int getStringResource(Context ctx, String name) {\n\t\treturn ctx.getResources().getIdentifier(name, \"string\", ctx.getPackageName());\n\t}\n\tstatic public int getLayoutResource(Context ctx, String name) {\n\t\treturn ctx.getResources().getIdentifier(name, \"layout\", ctx.getPackageName());\n\t}\n\tstatic public int getIdResource(Context ctx, String name) {\n\t\treturn ctx.getResources().getIdentifier(name, \"id\", ctx.getPackageName());\n\t}\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/IDownloaderClient.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader;\n\nimport android.os.Messenger;\n\n/**\n * This interface should be implemented by the client activity for the\n * downloader. It is used to pass status from the service to the client.\n */\npublic interface IDownloaderClient {\n    static final int STATE_IDLE = 1;\n    static final int STATE_FETCHING_URL = 2;\n    static final int STATE_CONNECTING = 3;\n    static final int STATE_DOWNLOADING = 4;\n    static final int STATE_COMPLETED = 5;\n\n    static final int STATE_PAUSED_NETWORK_UNAVAILABLE = 6;\n    static final int STATE_PAUSED_BY_REQUEST = 7;\n\n    /**\n     * Both STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION and\n     * STATE_PAUSED_NEED_CELLULAR_PERMISSION imply that Wi-Fi is unavailable and\n     * cellular permission will restart the service. Wi-Fi disabled means that\n     * the Wi-Fi manager is returning that Wi-Fi is not enabled, while in the\n     * other case Wi-Fi is enabled but not available.\n     */\n    static final int STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION = 8;\n    static final int STATE_PAUSED_NEED_CELLULAR_PERMISSION = 9;\n\n    /**\n     * Both STATE_PAUSED_WIFI_DISABLED and STATE_PAUSED_NEED_WIFI imply that\n     * Wi-Fi is unavailable and cellular permission will NOT restart the\n     * service. Wi-Fi disabled means that the Wi-Fi manager is returning that\n     * Wi-Fi is not enabled, while in the other case Wi-Fi is enabled but not\n     * available.\n     * <p>\n     * The service does not return these values. We recommend that app\n     * developers with very large payloads do not allow these payloads to be\n     * downloaded over cellular connections.\n     */\n    static final int STATE_PAUSED_WIFI_DISABLED = 10;\n    static final int STATE_PAUSED_NEED_WIFI = 11;\n\n    static final int STATE_PAUSED_ROAMING = 12;\n\n    /**\n     * Scary case. We were on a network that redirected us to another website\n     * that delivered us the wrong file.\n     */\n    static final int STATE_PAUSED_NETWORK_SETUP_FAILURE = 13;\n\n    static final int STATE_PAUSED_SDCARD_UNAVAILABLE = 14;\n\n    static final int STATE_FAILED_UNLICENSED = 15;\n    static final int STATE_FAILED_FETCHING_URL = 16;\n    static final int STATE_FAILED_SDCARD_FULL = 17;\n    static final int STATE_FAILED_CANCELED = 18;\n\n    static final int STATE_FAILED = 19;\n\n    /**\n     * Called internally by the stub when the service is bound to the client.\n     * <p>\n     * Critical implementation detail. In onServiceConnected we create the\n     * remote service and marshaler. This is how we pass the client information\n     * back to the service so the client can be properly notified of changes. We\n     * must do this every time we reconnect to the service.\n     * <p>\n     * That is, when you receive this callback, you should call\n     * {@link DownloaderServiceMarshaller#CreateProxy} to instantiate a member\n     * instance of {@link IDownloaderService}, then call\n     * {@link IDownloaderService#onClientUpdated} with the Messenger retrieved\n     * from your {@link IStub} proxy object.\n     * \n     * @param m the service Messenger. This Messenger is used to call the\n     *            service API from the client.\n     */\n    void onServiceConnected(Messenger m);\n\n    /**\n     * Called when the download state changes. Depending on the state, there may\n     * be user requests. The service is free to change the download state in the\n     * middle of a user request, so the client should be able to handle this.\n     * <p>\n     * The Downloader Library includes a collection of string resources that\n     * correspond to each of the states, which you can use to provide users a\n     * useful message based on the state provided in this callback. To fetch the\n     * appropriate string for a state, call\n     * {@link Helpers#getDownloaderStringResourceIDFromState}.\n     * <p>\n     * What this means to the developer: The application has gotten a message\n     * that the download has paused due to lack of WiFi. The developer should\n     * then show UI asking the user if they want to enable downloading over\n     * cellular connections with appropriate warnings. If the application\n     * suddenly starts downloading, the application should revert to showing the\n     * progress again, rather than leaving up the download over cellular UI up.\n     * \n     * @param newState one of the STATE_* values defined in IDownloaderClient\n     */\n    void onDownloadStateChanged(int newState);\n\n    /**\n     * Shows the download progress. This is intended to be used to fill out a\n     * client UI. This progress should only be shown in a few states such as\n     * STATE_DOWNLOADING.\n     * \n     * @param progress the DownloadProgressInfo object containing the current\n     *            progress of all downloads.\n     */\n    void onDownloadProgress(DownloadProgressInfo progress);\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/IDownloaderService.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader;\n\nimport com.google.android.vending.expansion.downloader.impl.DownloaderService;\nimport android.os.Messenger;\n\n/**\n * This interface is implemented by the DownloaderService and by the\n * DownloaderServiceMarshaller. It contains functions to control the service.\n * When a client binds to the service, it must call the onClientUpdated\n * function.\n * <p>\n * You can acquire a proxy that implements this interface for your service by\n * calling {@link DownloaderServiceMarshaller#CreateProxy} during the\n * {@link IDownloaderClient#onServiceConnected} callback. At which point, you\n * should immediately call {@link #onClientUpdated}.\n */\npublic interface IDownloaderService {\n    /**\n     * Set this flag in response to the\n     * IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION state and then\n     * call RequestContinueDownload to resume a download\n     */\n    public static final int FLAGS_DOWNLOAD_OVER_CELLULAR = 1;\n\n    /**\n     * Request that the service abort the current download. The service should\n     * respond by changing the state to {@link IDownloaderClient.STATE_ABORTED}.\n     */\n    void requestAbortDownload();\n\n    /**\n     * Request that the service pause the current download. The service should\n     * respond by changing the state to\n     * {@link IDownloaderClient.STATE_PAUSED_BY_REQUEST}.\n     */\n    void requestPauseDownload();\n\n    /**\n     * Request that the service continue a paused download, when in any paused\n     * or failed state, including\n     * {@link IDownloaderClient.STATE_PAUSED_BY_REQUEST}.\n     */\n    void requestContinueDownload();\n\n    /**\n     * Set the flags for this download (e.g.\n     * {@link DownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR}).\n     * \n     * @param flags\n     */\n    void setDownloadFlags(int flags);\n\n    /**\n     * Requests that the download status be sent to the client.\n     */\n    void requestDownloadStatus();\n\n    /**\n     * Call this when you get {@link\n     * IDownloaderClient.onServiceConnected(Messenger m)} from the\n     * DownloaderClient to register the client with the service. It will\n     * automatically send the current status to the client.\n     * \n     * @param clientMessenger\n     */\n    void onClientUpdated(Messenger clientMessenger);\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/IStub.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader;\n\nimport android.content.Context;\nimport android.os.Messenger;\n\n/**\n * This is the interface that is used to connect/disconnect from the downloader\n * service.\n * <p>\n * You should get a proxy object that implements this interface by calling\n * {@link DownloaderClientMarshaller#CreateStub} in your activity when the\n * downloader service starts. Then, call {@link #connect} during your activity's\n * onResume() and call {@link #disconnect} during onStop().\n * <p>\n * Then during the {@link IDownloaderClient#onServiceConnected} callback, you\n * should call {@link #getMessenger} to pass the stub's Messenger object to\n * {@link IDownloaderService#onClientUpdated}.\n */\npublic interface IStub {\n    Messenger getMessenger();\n\n    void connect(Context c);\n\n    void disconnect(Context c);\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/SystemFacade.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader;\n\nimport android.app.Notification;\nimport android.app.NotificationManager;\nimport android.content.Context;\nimport android.content.Intent;\nimport android.content.pm.PackageManager.NameNotFoundException;\nimport android.net.ConnectivityManager;\nimport android.net.NetworkInfo;\nimport android.telephony.TelephonyManager;\nimport android.util.Log;\n\n/**\n * Contains useful helper functions, typically tied to the application context.\n */\nclass SystemFacade {\n    private Context mContext;\n    private NotificationManager mNotificationManager;\n\n    public SystemFacade(Context context) {\n        mContext = context;\n        mNotificationManager = (NotificationManager)\n                mContext.getSystemService(Context.NOTIFICATION_SERVICE);\n    }\n\n    public long currentTimeMillis() {\n        return System.currentTimeMillis();\n    }\n\n    public Integer getActiveNetworkType() {\n        ConnectivityManager connectivity =\n                (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);\n        if (connectivity == null) {\n            Log.w(Constants.TAG, \"couldn't get connectivity manager\");\n            return null;\n        }\n\n        NetworkInfo activeInfo = connectivity.getActiveNetworkInfo();\n        if (activeInfo == null) {\n            if (Constants.LOGVV) {\n                Log.v(Constants.TAG, \"network is not available\");\n            }\n            return null;\n        }\n        return activeInfo.getType();\n    }\n\n    public boolean isNetworkRoaming() {\n        ConnectivityManager connectivity =\n                (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);\n        if (connectivity == null) {\n            Log.w(Constants.TAG, \"couldn't get connectivity manager\");\n            return false;\n        }\n\n        NetworkInfo info = connectivity.getActiveNetworkInfo();\n        boolean isMobile = (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE);\n        TelephonyManager tm = (TelephonyManager) mContext\n                .getSystemService(Context.TELEPHONY_SERVICE);\n        if (null == tm) {\n            Log.w(Constants.TAG, \"couldn't get telephony manager\");\n            return false;\n        }\n        boolean isRoaming = isMobile && tm.isNetworkRoaming();\n        if (Constants.LOGVV && isRoaming) {\n            Log.v(Constants.TAG, \"network is roaming\");\n        }\n        return isRoaming;\n    }\n\n    public Long getMaxBytesOverMobile() {\n        return (long) Integer.MAX_VALUE;\n    }\n\n    public Long getRecommendedMaxBytesOverMobile() {\n        return 2097152L;\n    }\n\n    public void sendBroadcast(Intent intent) {\n        mContext.sendBroadcast(intent);\n    }\n\n    public boolean userOwnsPackage(int uid, String packageName) throws NameNotFoundException {\n        return mContext.getPackageManager().getApplicationInfo(packageName, 0).uid == uid;\n    }\n\n    public void postNotification(long id, Notification notification) {\n        /**\n         * TODO: The system notification manager takes ints, not longs, as IDs,\n         * but the download manager uses IDs take straight from the database,\n         * which are longs. This will have to be dealt with at some point.\n         */\n        mNotificationManager.notify((int) id, notification);\n    }\n\n    public void cancelNotification(long id) {\n        mNotificationManager.cancel((int) id);\n    }\n\n    public void cancelAllNotifications() {\n        mNotificationManager.cancelAll();\n    }\n\n    public void startThread(Thread thread) {\n        thread.start();\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/impl/CustomIntentService.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader.impl;\n\nimport android.app.Service;\nimport android.content.Intent;\nimport android.os.Handler;\nimport android.os.HandlerThread;\nimport android.os.IBinder;\nimport android.os.Looper;\nimport android.os.Message;\nimport android.util.Log;\n\n/**\n * This service differs from IntentService in a few minor ways/ It will not\n * auto-stop itself after the intent is handled unless the target returns \"true\"\n * in should stop. Since the goal of this service is to handle a single kind of\n * intent, it does not queue up batches of intents of the same type.\n */\npublic abstract class CustomIntentService extends Service {\n    private String mName;\n    private boolean mRedelivery;\n    private volatile ServiceHandler mServiceHandler;\n    private volatile Looper mServiceLooper;\n    private static final String LOG_TAG = \"CustomIntentService\";\n    private static final int WHAT_MESSAGE = -10;\n\n    public CustomIntentService(String paramString) {\n        this.mName = paramString;\n    }\n\n    @Override\n    public IBinder onBind(Intent paramIntent) {\n        return null;\n    }\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        HandlerThread localHandlerThread = new HandlerThread(\"IntentService[\"\n                + this.mName + \"]\");\n        localHandlerThread.start();\n        this.mServiceLooper = localHandlerThread.getLooper();\n        this.mServiceHandler = new ServiceHandler(this.mServiceLooper);\n    }\n\n    @Override\n    public void onDestroy() {\n        Thread localThread = this.mServiceLooper.getThread();\n        if ((localThread != null) && (localThread.isAlive())) {\n            localThread.interrupt();\n        }\n        this.mServiceLooper.quit();\n        Log.d(LOG_TAG, \"onDestroy\");\n    }\n\n    protected abstract void onHandleIntent(Intent paramIntent);\n\n    protected abstract boolean shouldStop();\n\n    @Override\n    public void onStart(Intent paramIntent, int startId) {\n        if (!this.mServiceHandler.hasMessages(WHAT_MESSAGE)) {\n            Message localMessage = this.mServiceHandler.obtainMessage();\n            localMessage.arg1 = startId;\n            localMessage.obj = paramIntent;\n            localMessage.what = WHAT_MESSAGE;\n            this.mServiceHandler.sendMessage(localMessage);\n        }\n    }\n\n    @Override\n    public int onStartCommand(Intent paramIntent, int flags, int startId) {\n        onStart(paramIntent, startId);\n        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;\n    }\n\n    public void setIntentRedelivery(boolean enabled) {\n        this.mRedelivery = enabled;\n    }\n\n    private final class ServiceHandler extends Handler {\n        public ServiceHandler(Looper looper) {\n            super(looper);\n        }\n\n        @Override\n        public void handleMessage(Message paramMessage) {\n            CustomIntentService.this\n                    .onHandleIntent((Intent) paramMessage.obj);\n            if (shouldStop()) {\n                Log.d(LOG_TAG, \"stopSelf\");\n                CustomIntentService.this.stopSelf(paramMessage.arg1);\n                Log.d(LOG_TAG, \"afterStopSelf\");\n            }\n        }\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/impl/DownloadInfo.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader.impl;\n\nimport com.google.android.vending.expansion.downloader.Constants;\nimport com.google.android.vending.expansion.downloader.Helpers;\n\nimport android.util.Log;\n\n/**\n * Representation of information about an individual download from the database.\n */\npublic class DownloadInfo {\n    public String mUri;\n    public final int mIndex;\n    public final String mFileName;\n    public String mETag;\n    public long mTotalBytes;\n    public long mCurrentBytes;\n    public long mLastMod;\n    public int mStatus;\n    public int mControl;\n    public int mNumFailed;\n    public int mRetryAfter;\n    public int mRedirectCount;\n\n    boolean mInitialized;\n\n    public int mFuzz;\n\n    public DownloadInfo(int index, String fileName, String pkg) {\n        mFuzz = Helpers.sRandom.nextInt(1001);\n        mFileName = fileName;\n        mIndex = index;\n    }\n\n    public void resetDownload() {\n        mCurrentBytes = 0;\n        mETag = \"\";\n        mLastMod = 0;\n        mStatus = 0;\n        mControl = 0;\n        mNumFailed = 0;\n        mRetryAfter = 0;\n        mRedirectCount = 0;\n    }\n\n    /**\n     * Returns the time when a download should be restarted.\n     */\n    public long restartTime(long now) {\n        if (mNumFailed == 0) {\n            return now;\n        }\n        if (mRetryAfter > 0) {\n            return mLastMod + mRetryAfter;\n        }\n        return mLastMod +\n                Constants.RETRY_FIRST_DELAY *\n                (1000 + mFuzz) * (1 << (mNumFailed - 1));\n    }\n\n    public void logVerboseInfo() {\n        Log.v(Constants.TAG, \"Service adding new entry\");\n        Log.v(Constants.TAG, \"FILENAME: \" + mFileName);\n        Log.v(Constants.TAG, \"URI     : \" + mUri);\n        Log.v(Constants.TAG, \"FILENAME: \" + mFileName);\n        Log.v(Constants.TAG, \"CONTROL : \" + mControl);\n        Log.v(Constants.TAG, \"STATUS  : \" + mStatus);\n        Log.v(Constants.TAG, \"FAILED_C: \" + mNumFailed);\n        Log.v(Constants.TAG, \"RETRY_AF: \" + mRetryAfter);\n        Log.v(Constants.TAG, \"REDIRECT: \" + mRedirectCount);\n        Log.v(Constants.TAG, \"LAST_MOD: \" + mLastMod);\n        Log.v(Constants.TAG, \"TOTAL   : \" + mTotalBytes);\n        Log.v(Constants.TAG, \"CURRENT : \" + mCurrentBytes);\n        Log.v(Constants.TAG, \"ETAG    : \" + mETag);\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/impl/DownloadNotification.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader.impl;\n\nimport com.google.android.vending.expansion.downloader.DownloadProgressInfo;\nimport com.google.android.vending.expansion.downloader.DownloaderClientMarshaller;\nimport com.google.android.vending.expansion.downloader.Helpers;\nimport com.google.android.vending.expansion.downloader.IDownloaderClient;\n\nimport android.app.Notification;\nimport android.app.NotificationManager;\nimport android.app.PendingIntent;\nimport android.content.Context;\nimport android.os.Build;\nimport android.os.Messenger;\n\n/**\n * This class handles displaying the notification associated with the download\n * queue going on in the download manager. It handles multiple status types;\n * Some require user interaction and some do not. Some of the user interactions\n * may be transient. (for example: the user is queried to continue the download\n * on 3G when it started on WiFi, but then the phone locks onto WiFi again so\n * the prompt automatically goes away)\n * <p/>\n * The application interface for the downloader also needs to understand and\n * handle these transient states.\n */\npublic class DownloadNotification implements IDownloaderClient {\n\n    private int mState;\n    private final Context mContext;\n    private final NotificationManager mNotificationManager;\n    private CharSequence mCurrentTitle;\n\n    private IDownloaderClient mClientProxy;\n    private Notification.Builder mActiveDownloadBuilder;\n    private Notification.Builder mBuilder;\n    private Notification.Builder mCurrentBuilder;\n    private CharSequence mLabel;\n    private String mCurrentText;\n    private DownloadProgressInfo mProgressInfo;\n    private PendingIntent mContentIntent;\n\n    private static final String LOGTAG = \"DownloadNotification\";\n    private static final int NOTIFICATION_ID = LOGTAG.hashCode();\n\n    public void setClientIntent(PendingIntent clientIntent) {\n        this.mBuilder.setContentIntent(clientIntent);\n        this.mActiveDownloadBuilder.setContentIntent(clientIntent);\n        this.mContentIntent = clientIntent;\n    }\n\n    public void resendState() {\n        if (null != mClientProxy) {\n            mClientProxy.onDownloadStateChanged(mState);\n        }\n    }\n\n    @Override\n    public void onDownloadStateChanged(int newState) {\n        if (null != mClientProxy) {\n            mClientProxy.onDownloadStateChanged(newState);\n        }\n        if (newState != mState) {\n            mState = newState;\n            if (newState == IDownloaderClient.STATE_IDLE || null == mContentIntent) {\n                return;\n            }\n            int stringDownloadID;\n            int iconResource;\n            boolean ongoingEvent;\n\n            // get the new title string and paused text\n            switch (newState) {\n                case 0:\n                    iconResource = android.R.drawable.stat_sys_warning;\n                    stringDownloadID = Helpers.getStringResource(mContext, \"state_unknown\");\n                    ongoingEvent = false;\n                    break;\n\n                case IDownloaderClient.STATE_DOWNLOADING:\n                    iconResource = android.R.drawable.stat_sys_download;\n                    stringDownloadID = Helpers.getDownloaderStringResourceIDFromState(mContext, newState);\n                    ongoingEvent = true;\n                    break;\n\n                case IDownloaderClient.STATE_FETCHING_URL:\n                case IDownloaderClient.STATE_CONNECTING:\n                    iconResource = android.R.drawable.stat_sys_download_done;\n                    stringDownloadID = Helpers.getDownloaderStringResourceIDFromState(mContext, newState);\n                    ongoingEvent = true;\n                    break;\n\n                case IDownloaderClient.STATE_COMPLETED:\n                case IDownloaderClient.STATE_PAUSED_BY_REQUEST:\n                    iconResource = android.R.drawable.stat_sys_download_done;\n                    stringDownloadID = Helpers.getDownloaderStringResourceIDFromState(mContext, newState);\n                    ongoingEvent = false;\n                    break;\n\n                case IDownloaderClient.STATE_FAILED:\n                case IDownloaderClient.STATE_FAILED_CANCELED:\n                case IDownloaderClient.STATE_FAILED_FETCHING_URL:\n                case IDownloaderClient.STATE_FAILED_SDCARD_FULL:\n                case IDownloaderClient.STATE_FAILED_UNLICENSED:\n                    iconResource = android.R.drawable.stat_sys_warning;\n                    stringDownloadID = Helpers.getDownloaderStringResourceIDFromState(mContext, newState);\n                    ongoingEvent = false;\n                    break;\n\n                default:\n                    iconResource = android.R.drawable.stat_sys_warning;\n                    stringDownloadID = Helpers.getDownloaderStringResourceIDFromState(mContext, newState);\n                    ongoingEvent = true;\n                    break;\n            }\n\n            mCurrentText = mContext.getString(stringDownloadID);\n            mCurrentTitle = mLabel;\n            mCurrentBuilder.setTicker(mLabel + \": \" + mCurrentText)\n            .setSmallIcon(iconResource)\n            .setContentTitle(mCurrentTitle)\n            .setContentText(mCurrentText);\n            if (ongoingEvent) {\n                mCurrentBuilder.setOngoing(true);\n            } else {\n                mCurrentBuilder.setOngoing(false);\n                mCurrentBuilder.setAutoCancel(true);\n            }\n            Notification notification;\n            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {\n                notification = mCurrentBuilder.build();\n            } else {\n                notification = mCurrentBuilder.getNotification();\n            }\n            mNotificationManager.notify(NOTIFICATION_ID, notification);\n        }\n    }\n\n    @Override\n    public void onDownloadProgress(DownloadProgressInfo progress) {\n        mProgressInfo = progress;\n        if (null != mClientProxy) {\n            mClientProxy.onDownloadProgress(progress);\n        }\n        if (progress.mOverallTotal <= 0) {\n            // we just show the text\n            mBuilder.setTicker(mCurrentTitle)\n            .setSmallIcon(android.R.drawable.stat_sys_download)\n            .setContentTitle(mCurrentTitle)\n            .setContentText(mCurrentText);\n            mCurrentBuilder = mBuilder;\n        } else {\n            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {\n                mActiveDownloadBuilder.setProgress((int) progress.mOverallTotal, (int) progress.mOverallProgress, false);\n            }\n            mActiveDownloadBuilder.setContentText(Helpers.getDownloadProgressString(progress.mOverallProgress, progress.mOverallTotal))\n            .setSmallIcon(android.R.drawable.stat_sys_download)\n            .setTicker(mLabel + \": \" + mCurrentText)\n            .setContentTitle(mLabel)\n            .setContentInfo(mContext.getString(Helpers.getStringResource(mContext, \"time_remaining_notification\"),\n                    Helpers.getTimeRemaining(progress.mTimeRemaining)));\n            mCurrentBuilder = mActiveDownloadBuilder;\n        }\n        Notification notification;\n        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {\n            notification = mCurrentBuilder.build();\n        } else {\n            notification = mCurrentBuilder.getNotification();\n        }\n        mNotificationManager.notify(NOTIFICATION_ID, notification);\n    }\n\n    /**\n     * Called in response to onClientUpdated. Creates a new proxy and notifies\n     * it of the current state.\n     *\n     * @param msg the client Messenger to notify\n     */\n    public void setMessenger(Messenger msg) {\n        mClientProxy = DownloaderClientMarshaller.CreateProxy(msg);\n        if (null != mProgressInfo) {\n            mClientProxy.onDownloadProgress(mProgressInfo);\n        }\n        if (mState != -1) {\n            mClientProxy.onDownloadStateChanged(mState);\n        }\n    }\n\n    /**\n     * Constructor\n     *\n     * @param ctx The context to use to obtain access to the Notification\n     *            Service\n     */\n    DownloadNotification(Context ctx, CharSequence applicationLabel) {\n        mState = -1;\n        mContext = ctx;\n        mLabel = applicationLabel;\n        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);\n        mActiveDownloadBuilder = new Notification.Builder(ctx);\n        mBuilder = new Notification.Builder(ctx);\n\n        // Set Notification category and priorities to something that makes sense for a long\n        // lived background task.\n        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {\n            mActiveDownloadBuilder.setPriority(Notification.PRIORITY_LOW);\n            mBuilder.setPriority(Notification.PRIORITY_LOW);\n        }\n        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {\n            mActiveDownloadBuilder.setCategory(Notification.CATEGORY_PROGRESS);\n            mBuilder.setCategory(Notification.CATEGORY_PROGRESS);\n        }\n\n        mCurrentBuilder = mBuilder;\n    }\n\n    @Override\n    public void onServiceConnected(Messenger m) {\n    }\n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/impl/DownloadThread.java",
    "content": "/*\n * Copyright (C) 2015 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader.impl;\n\nimport com.google.android.vending.expansion.downloader.Constants;\nimport com.google.android.vending.expansion.downloader.Helpers;\nimport com.google.android.vending.expansion.downloader.IDownloaderClient;\n\nimport android.content.Context;\nimport android.os.PowerManager;\nimport android.os.Process;\nimport android.util.Log;\n\nimport java.io.File;\nimport java.io.FileNotFoundException;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.SyncFailedException;\nimport java.net.HttpURLConnection;\nimport java.net.URL;\nimport java.util.Locale;\n\n/**\n * Runs an actual download\n */\npublic class DownloadThread {\n\n    private Context mContext;\n    private DownloadInfo mInfo;\n    private DownloaderService mService;\n    private final DownloadsDB mDB;\n    private final DownloadNotification mNotification;\n    private String mUserAgent;\n\n    public DownloadThread(DownloadInfo info, DownloaderService service,\n            DownloadNotification notification) {\n        mContext = service;\n        mInfo = info;\n        mService = service;\n        mNotification = notification;\n        mDB = DownloadsDB.getDB(service);\n        mUserAgent = \"APKXDL (Linux; U; Android \" + android.os.Build.VERSION.RELEASE + \";\"\n                + Locale.getDefault().toString() + \"; \" + android.os.Build.DEVICE + \"/\"\n                + android.os.Build.ID + \")\" +\n                service.getPackageName();\n    }\n\n    /**\n     * Returns the default user agent\n     */\n    private String userAgent() {\n        return mUserAgent;\n    }\n\n    /**\n     * State for the entire run() method.\n     */\n    private static class State {\n        public String mFilename;\n        public FileOutputStream mStream;\n        public boolean mCountRetry = false;\n        public int mRetryAfter = 0;\n        public int mRedirectCount = 0;\n        public String mNewUri;\n        public boolean mGotData = false;\n        public String mRequestUri;\n\n        public State(DownloadInfo info, DownloaderService service) {\n            mRedirectCount = info.mRedirectCount;\n            mRequestUri = info.mUri;\n            mFilename = service.generateTempSaveFileName(info.mFileName);\n        }\n    }\n\n    /**\n     * State within executeDownload()\n     */\n    private static class InnerState {\n        public int mBytesSoFar = 0;\n        public int mBytesThisSession = 0;\n        public String mHeaderETag;\n        public boolean mContinuingDownload = false;\n        public String mHeaderContentLength;\n        public String mHeaderContentDisposition;\n        public String mHeaderContentLocation;\n        public int mBytesNotified = 0;\n        public long mTimeLastNotification = 0;\n    }\n\n    /**\n     * Raised from methods called by run() to indicate that the current request\n     * should be stopped immediately. Note the message passed to this exception\n     * will be logged and therefore must be guaranteed not to contain any PII,\n     * meaning it generally can't include any information about the request URI,\n     * headers, or destination filename.\n     */\n    private class StopRequest extends Throwable {\n    \t\n        private static final long serialVersionUID = 6338592678988347973L;\n        public int mFinalStatus;\n\n        public StopRequest(int finalStatus, String message) {\n            super(message);\n            mFinalStatus = finalStatus;\n        }\n\n        public StopRequest(int finalStatus, String message, Throwable throwable) {\n            super(message, throwable);\n            mFinalStatus = finalStatus;\n        }\n    }\n\n    /**\n     * Raised from methods called by executeDownload() to indicate that the\n     * download should be retried immediately.\n     */\n    private class RetryDownload extends Throwable {\n\n        private static final long serialVersionUID = 6196036036517540229L;\n    }\n\n    /**\n     * Executes the download in a separate thread\n     */\n    public void run() {\n        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);\n\n        State state = new State(mInfo, mService);\n        PowerManager.WakeLock wakeLock = null;\n        int finalStatus = DownloaderService.STATUS_UNKNOWN_ERROR;\n\n        try {\n            PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);\n            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);\n            wakeLock.acquire();\n\n            if (Constants.LOGV) {\n                Log.v(Constants.TAG, \"initiating download for \" + mInfo.mFileName);\n                Log.v(Constants.TAG, \"  at \" + mInfo.mUri);\n            }\n\n            boolean finished = false;\n            while (!finished) {\n                if (Constants.LOGV) {\n                    Log.v(Constants.TAG, \"initiating download for \" + mInfo.mFileName);\n                    Log.v(Constants.TAG, \"  at \" + mInfo.mUri);\n                }\n                // Set or unset proxy, which may have changed since last GET\n                // request.\n                // setDefaultProxy() supports null as proxy parameter.\n                URL url = new URL(state.mRequestUri);\n                HttpURLConnection request = (HttpURLConnection)url.openConnection();\n                request.setRequestProperty(\"User-Agent\", userAgent());\n                try {\n                    executeDownload(state, request);\n                    finished = true;\n                } catch (RetryDownload exc) {\n                    // fall through\n                } finally {\n                    request.disconnect();\n                    request = null;\n                }\n            }\n\n            if (Constants.LOGV) {\n                Log.v(Constants.TAG, \"download completed for \" + mInfo.mFileName);\n                Log.v(Constants.TAG, \"  at \" + mInfo.mUri);\n            }\n            finalizeDestinationFile(state);\n            finalStatus = DownloaderService.STATUS_SUCCESS;\n        } catch (StopRequest error) {\n            // remove the cause before printing, in case it contains PII\n            Log.w(Constants.TAG,\n                    \"Aborting request for download \" + mInfo.mFileName + \": \" + error.getMessage());\n            error.printStackTrace();\n            finalStatus = error.mFinalStatus;\n            // fall through to finally block\n        } catch (Throwable ex) { // sometimes the socket code throws unchecked\n                                 // exceptions\n            Log.w(Constants.TAG, \"Exception for \" + mInfo.mFileName + \": \" + ex);\n            finalStatus = DownloaderService.STATUS_UNKNOWN_ERROR;\n            // falls through to the code that reports an error\n        } finally {\n            if (wakeLock != null) {\n                wakeLock.release();\n                wakeLock = null;\n            }\n            cleanupDestination(state, finalStatus);\n            notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter,\n                    state.mRedirectCount, state.mGotData, state.mFilename);\n        }\n    }\n\n    /**\n     * Fully execute a single download request - setup and send the request,\n     * handle the response, and transfer the data to the destination file.\n     */\n    private void executeDownload(State state, HttpURLConnection request)\n            throws StopRequest, RetryDownload {\n        InnerState innerState = new InnerState();\n        byte data[] = new byte[Constants.BUFFER_SIZE];\n\n        checkPausedOrCanceled(state);\n\n        setupDestinationFile(state, innerState);\n        addRequestHeaders(innerState, request);\n\n        // check just before sending the request to avoid using an invalid\n        // connection at all\n        checkConnectivity(state);\n\n        mNotification.onDownloadStateChanged(IDownloaderClient.STATE_CONNECTING);\n        int responseCode = sendRequest(state, request);\n        handleExceptionalStatus(state, innerState, request, responseCode);\n\n        if (Constants.LOGV) {\n            Log.v(Constants.TAG, \"received response for \" + mInfo.mUri);\n        }\n\n        processResponseHeaders(state, innerState, request);\n        InputStream entityStream = openResponseEntity(state, request);\n        mNotification.onDownloadStateChanged(IDownloaderClient.STATE_DOWNLOADING);\n        transferData(state, innerState, data, entityStream);\n    }\n\n    /**\n     * Check if current connectivity is valid for this request.\n     */\n    private void checkConnectivity(State state) throws StopRequest {\n        switch (mService.getNetworkAvailabilityState(mDB)) {\n            case DownloaderService.NETWORK_OK:\n                return;\n            case DownloaderService.NETWORK_NO_CONNECTION:\n                throw new StopRequest(DownloaderService.STATUS_WAITING_FOR_NETWORK,\n                        \"waiting for network to return\");\n            case DownloaderService.NETWORK_TYPE_DISALLOWED_BY_REQUESTOR:\n                throw new StopRequest(\n                        DownloaderService.STATUS_QUEUED_FOR_WIFI_OR_CELLULAR_PERMISSION,\n                        \"waiting for wifi or for download over cellular to be authorized\");\n            case DownloaderService.NETWORK_CANNOT_USE_ROAMING:\n                throw new StopRequest(DownloaderService.STATUS_WAITING_FOR_NETWORK,\n                        \"roaming is not allowed\");\n            case DownloaderService.NETWORK_UNUSABLE_DUE_TO_SIZE:\n                throw new StopRequest(DownloaderService.STATUS_QUEUED_FOR_WIFI, \"waiting for wifi\");\n        }\n    }\n\n    /**\n     * Transfer as much data as possible from the HTTP response to the\n     * destination file.\n     *\n     * @param data buffer to use to read data\n     * @param entityStream stream for reading the HTTP response entity\n     */\n    private void transferData(State state, InnerState innerState, byte[] data,\n            InputStream entityStream) throws StopRequest {\n        for (;;) {\n            int bytesRead = readFromResponse(state, innerState, data, entityStream);\n            if (bytesRead == -1) { // success, end of stream already reached\n                handleEndOfStream(state, innerState);\n                return;\n            }\n\n            state.mGotData = true;\n            writeDataToDestination(state, data, bytesRead);\n            innerState.mBytesSoFar += bytesRead;\n            innerState.mBytesThisSession += bytesRead;\n            reportProgress(state, innerState);\n\n            checkPausedOrCanceled(state);\n        }\n    }\n\n    /**\n     * Called after a successful completion to take any necessary action on the\n     * downloaded file.\n     */\n    private void finalizeDestinationFile(State state) throws StopRequest {\n        syncDestination(state);\n        String tempFilename = state.mFilename;\n        String finalFilename = Helpers.generateSaveFileName(mService, mInfo.mFileName);\n        if (!state.mFilename.equals(finalFilename)) {\n            File startFile = new File(tempFilename);\n            File destFile = new File(finalFilename);\n            if (mInfo.mTotalBytes != -1 && mInfo.mCurrentBytes == mInfo.mTotalBytes) {\n                if (!startFile.renameTo(destFile)) {\n                    throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,\n                            \"unable to finalize destination file\");\n                }\n            } else {\n                throw new StopRequest(DownloaderService.STATUS_FILE_DELIVERED_INCORRECTLY,\n                        \"file delivered with incorrect size. probably due to network not browser configured\");\n            }\n        }\n    }\n\n    /**\n     * Called just before the thread finishes, regardless of status, to take any\n     * necessary action on the downloaded file.\n     */\n    private void cleanupDestination(State state, int finalStatus) {\n        closeDestination(state);\n        if (state.mFilename != null && DownloaderService.isStatusError(finalStatus)) {\n            new File(state.mFilename).delete();\n            state.mFilename = null;\n        }\n    }\n\n    /**\n     * Sync the destination file to storage.\n     */\n    private void syncDestination(State state) {\n        FileOutputStream downloadedFileStream = null;\n        try {\n            downloadedFileStream = new FileOutputStream(state.mFilename, true);\n            downloadedFileStream.getFD().sync();\n        } catch (FileNotFoundException ex) {\n            Log.w(Constants.TAG, \"file \" + state.mFilename + \" not found: \" + ex);\n        } catch (SyncFailedException ex) {\n            Log.w(Constants.TAG, \"file \" + state.mFilename + \" sync failed: \" + ex);\n        } catch (IOException ex) {\n            Log.w(Constants.TAG, \"IOException trying to sync \" + state.mFilename + \": \" + ex);\n        } catch (RuntimeException ex) {\n            Log.w(Constants.TAG, \"exception while syncing file: \", ex);\n        } finally {\n            if (downloadedFileStream != null) {\n                try {\n                    downloadedFileStream.close();\n                } catch (IOException ex) {\n                    Log.w(Constants.TAG, \"IOException while closing synced file: \", ex);\n                } catch (RuntimeException ex) {\n                    Log.w(Constants.TAG, \"exception while closing file: \", ex);\n                }\n            }\n        }\n    }\n\n    /**\n     * Close the destination output stream.\n     */\n    private void closeDestination(State state) {\n        try {\n            // close the file\n            if (state.mStream != null) {\n                state.mStream.close();\n                state.mStream = null;\n            }\n        } catch (IOException ex) {\n            if (Constants.LOGV) {\n                Log.v(Constants.TAG, \"exception when closing the file after download : \" + ex);\n            }\n            // nothing can really be done if the file can't be closed\n        }\n    }\n\n    /**\n     * Check if the download has been paused or canceled, stopping the request\n     * appropriately if it has been.\n     */\n    private void checkPausedOrCanceled(State state) throws StopRequest {\n        if (mService.getControl() == DownloaderService.CONTROL_PAUSED) {\n            int status = mService.getStatus();\n            switch (status) {\n                case DownloaderService.STATUS_PAUSED_BY_APP:\n                    throw new StopRequest(mService.getStatus(),\n                            \"download paused\");\n            }\n        }\n    }\n\n    /**\n     * Report download progress through the database if necessary.\n     */\n    private void reportProgress(State state, InnerState innerState) {\n        long now = System.currentTimeMillis();\n        if (innerState.mBytesSoFar - innerState.mBytesNotified\n                > Constants.MIN_PROGRESS_STEP\n                && now - innerState.mTimeLastNotification\n                > Constants.MIN_PROGRESS_TIME) {\n            // we store progress updates to the database here\n            mInfo.mCurrentBytes = innerState.mBytesSoFar;\n            mDB.updateDownloadCurrentBytes(mInfo);\n\n            innerState.mBytesNotified = innerState.mBytesSoFar;\n            innerState.mTimeLastNotification = now;\n\n            long totalBytesSoFar = innerState.mBytesThisSession + mService.mBytesSoFar;\n\n            if (Constants.LOGVV) {\n                Log.v(Constants.TAG, \"downloaded \" + mInfo.mCurrentBytes + \" out of \"\n                        + mInfo.mTotalBytes);\n                Log.v(Constants.TAG, \"     total \" + totalBytesSoFar + \" out of \"\n                        + mService.mTotalLength);\n            }\n\n            mService.notifyUpdateBytes(totalBytesSoFar);\n        }\n    }\n\n    /**\n     * Write a data buffer to the destination file.\n     *\n     * @param data buffer containing the data to write\n     * @param bytesRead how many bytes to write from the buffer\n     */\n    private void writeDataToDestination(State state, byte[] data, int bytesRead)\n            throws StopRequest {\n        for (;;) {\n            try {\n                if (state.mStream == null) {\n                    state.mStream = new FileOutputStream(state.mFilename, true);\n                }\n                state.mStream.write(data, 0, bytesRead);\n                // we close after every write --- this may be too inefficient\n                closeDestination(state);\n                return;\n            } catch (IOException ex) {\n                if (!Helpers.isExternalMediaMounted()) {\n                    throw new StopRequest(DownloaderService.STATUS_DEVICE_NOT_FOUND_ERROR,\n                            \"external media not mounted while writing destination file\");\n                }\n\n                long availableBytes =\n                        Helpers.getAvailableBytes(Helpers.getFilesystemRoot(state.mFilename));\n                if (availableBytes < bytesRead) {\n                    throw new StopRequest(DownloaderService.STATUS_INSUFFICIENT_SPACE_ERROR,\n                            \"insufficient space while writing destination file\", ex);\n                }\n                throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,\n                        \"while writing destination file: \" + ex.toString(), ex);\n            }\n        }\n    }\n\n    /**\n     * Called when we've reached the end of the HTTP response stream, to update\n     * the database and check for consistency.\n     */\n    private void handleEndOfStream(State state, InnerState innerState) throws StopRequest {\n        mInfo.mCurrentBytes = innerState.mBytesSoFar;\n        // this should always be set from the market\n        // if ( innerState.mHeaderContentLength == null ) {\n        // mInfo.mTotalBytes = innerState.mBytesSoFar;\n        // }\n        mDB.updateDownload(mInfo);\n\n        boolean lengthMismatched = (innerState.mHeaderContentLength != null)\n                && (innerState.mBytesSoFar != Integer.parseInt(innerState.mHeaderContentLength));\n        if (lengthMismatched) {\n            if (cannotResume(innerState)) {\n                throw new StopRequest(DownloaderService.STATUS_CANNOT_RESUME,\n                        \"mismatched content length\");\n            } else {\n                throw new StopRequest(getFinalStatusForHttpError(state),\n                        \"closed socket before end of file\");\n            }\n        }\n    }\n\n    private boolean cannotResume(InnerState innerState) {\n        return innerState.mBytesSoFar > 0 && innerState.mHeaderETag == null;\n    }\n\n    /**\n     * Read some data from the HTTP response stream, handling I/O errors.\n     *\n     * @param data buffer to use to read data\n     * @param entityStream stream for reading the HTTP response entity\n     * @return the number of bytes actually read or -1 if the end of the stream\n     *         has been reached\n     */\n    private int readFromResponse(State state, InnerState innerState, byte[] data,\n            InputStream entityStream) throws StopRequest {\n        try {\n            return entityStream.read(data);\n        } catch (IOException ex) {\n            logNetworkState();\n            mInfo.mCurrentBytes = innerState.mBytesSoFar;\n            mDB.updateDownload(mInfo);\n            if (cannotResume(innerState)) {\n                String message = \"while reading response: \" + ex.toString()\n                        + \", can't resume interrupted download with no ETag\";\n                throw new StopRequest(DownloaderService.STATUS_CANNOT_RESUME,\n                        message, ex);\n            } else {\n                throw new StopRequest(getFinalStatusForHttpError(state),\n                        \"while reading response: \" + ex.toString(), ex);\n            }\n        }\n    }\n\n    /**\n     * Open a stream for the HTTP response entity, handling I/O errors.\n     *\n     * @return an InputStream to read the response entity\n     */\n    private InputStream openResponseEntity(State state, HttpURLConnection response)\n            throws StopRequest {\n        try {\n            return response.getInputStream();\n        } catch (IOException ex) {\n            logNetworkState();\n            throw new StopRequest(getFinalStatusForHttpError(state),\n                    \"while getting entity: \" + ex.toString(), ex);\n        }\n    }\n\n    private void logNetworkState() {\n        if (Constants.LOGX) {\n            Log.i(Constants.TAG,\n                    \"Net \"\n                            + (mService.getNetworkAvailabilityState(mDB) == DownloaderService.NETWORK_OK ? \"Up\"\n                                    : \"Down\"));\n        }\n    }\n\n    /**\n     * Read HTTP response headers and take appropriate action, including setting\n     * up the destination file and updating the database.\n     */\n    private void processResponseHeaders(State state, InnerState innerState, HttpURLConnection response)\n            throws StopRequest {\n        if (innerState.mContinuingDownload) {\n            // ignore response headers on resume requests\n            return;\n        }\n\n        readResponseHeaders(state, innerState, response);\n\n        try {\n            state.mFilename = mService.generateSaveFile(mInfo.mFileName, mInfo.mTotalBytes);\n        } catch (DownloaderService.GenerateSaveFileError exc) {\n            throw new StopRequest(exc.mStatus, exc.mMessage);\n        }\n        try {\n            state.mStream = new FileOutputStream(state.mFilename);\n        } catch (FileNotFoundException exc) {\n            // make sure the directory exists\n            File pathFile = new File(Helpers.getSaveFilePath(mService));\n            try {\n                if (pathFile.mkdirs()) {\n                    state.mStream = new FileOutputStream(state.mFilename);\n                }\n            } catch (Exception ex) {\n                throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,\n                        \"while opening destination file: \" + exc.toString(), exc);\n            }\n        }\n        if (Constants.LOGV) {\n            Log.v(Constants.TAG, \"writing \" + mInfo.mUri + \" to \" + state.mFilename);\n        }\n\n        updateDatabaseFromHeaders(state, innerState);\n        // check connectivity again now that we know the total size\n        checkConnectivity(state);\n    }\n\n    /**\n     * Update necessary database fields based on values of HTTP response headers\n     * that have been read.\n     */\n    private void updateDatabaseFromHeaders(State state, InnerState innerState) {\n        mInfo.mETag = innerState.mHeaderETag;\n        mDB.updateDownload(mInfo);\n    }\n\n    /**\n     * Read headers from the HTTP response and store them into local state.\n     */\n    private void readResponseHeaders(State state, InnerState innerState, HttpURLConnection response)\n            throws StopRequest {\n        String value = response.getHeaderField(\"Content-Disposition\");\n        if (value != null) {\n            innerState.mHeaderContentDisposition = value;\n        }\n        value = response.getHeaderField(\"Content-Location\");\n        if (value != null) {\n            innerState.mHeaderContentLocation = value;\n        }\n        value = response.getHeaderField(\"ETag\");\n        if (value != null) {\n            innerState.mHeaderETag = value;\n        }\n        String headerTransferEncoding = null;\n        value = response.getHeaderField(\"Transfer-Encoding\");\n        if (value != null) {\n            headerTransferEncoding = value;\n        }\n        String headerContentType = null;\n        value = response.getHeaderField(\"Content-Type\");\n        if (value != null) {\n            headerContentType = value;\n            if (!headerContentType.equals(\"application/vnd.android.obb\")) {\n                throw new StopRequest(DownloaderService.STATUS_FILE_DELIVERED_INCORRECTLY,\n                        \"file delivered with incorrect Mime type\");\n            }\n        }\n\n        if (headerTransferEncoding == null) {\n            long contentLength = response.getContentLength();\n            if (value != null) {\n                // this is always set from Market\n                if (contentLength != -1 && contentLength != mInfo.mTotalBytes) {\n                    // we're most likely on a bad wifi connection -- we should\n                    // probably\n                    // also look at the mime type --- but the size mismatch is\n                    // enough\n                    // to tell us that something is wrong here\n                    Log.e(Constants.TAG, \"Incorrect file size delivered.\");\n                } else {\n                    innerState.mHeaderContentLength = Long.toString(contentLength);\n                }\n            }\n        } else {\n            // Ignore content-length with transfer-encoding - 2616 4.4 3\n            if (Constants.LOGVV) {\n                Log.v(Constants.TAG,\n                        \"ignoring content-length because of xfer-encoding\");\n            }\n        }\n        if (Constants.LOGVV) {\n            Log.v(Constants.TAG, \"Content-Disposition: \" +\n                    innerState.mHeaderContentDisposition);\n            Log.v(Constants.TAG, \"Content-Length: \" + innerState.mHeaderContentLength);\n            Log.v(Constants.TAG, \"Content-Location: \" + innerState.mHeaderContentLocation);\n            Log.v(Constants.TAG, \"ETag: \" + innerState.mHeaderETag);\n            Log.v(Constants.TAG, \"Transfer-Encoding: \" + headerTransferEncoding);\n        }\n\n        boolean noSizeInfo = innerState.mHeaderContentLength == null\n                && (headerTransferEncoding == null\n                || !headerTransferEncoding.equalsIgnoreCase(\"chunked\"));\n        if (noSizeInfo) {\n            throw new StopRequest(DownloaderService.STATUS_HTTP_DATA_ERROR,\n                    \"can't know size of download, giving up\");\n        }\n    }\n\n    /**\n     * Check the HTTP response status and handle anything unusual (e.g. not\n     * 200/206).\n     */\n    private void handleExceptionalStatus(State state, InnerState innerState, HttpURLConnection connection, int responseCode)\n            throws StopRequest, RetryDownload {\n        if (responseCode == 503 && mInfo.mNumFailed < Constants.MAX_RETRIES) {\n            handleServiceUnavailable(state, connection);\n        }\n        int expectedStatus = innerState.mContinuingDownload ? 206\n                : DownloaderService.STATUS_SUCCESS;\n        if (responseCode != expectedStatus) {\n            handleOtherStatus(state, innerState, responseCode);\n        } else {\n            // no longer redirected\n            state.mRedirectCount = 0;\n        }\n    }\n\n    /**\n     * Handle a status that we don't know how to deal with properly.\n     */\n    private void handleOtherStatus(State state, InnerState innerState, int statusCode)\n            throws StopRequest {\n        int finalStatus;\n        if (DownloaderService.isStatusError(statusCode)) {\n            finalStatus = statusCode;\n        } else if (statusCode >= 300 && statusCode < 400) {\n            finalStatus = DownloaderService.STATUS_UNHANDLED_REDIRECT;\n        } else if (innerState.mContinuingDownload && statusCode == DownloaderService.STATUS_SUCCESS) {\n            finalStatus = DownloaderService.STATUS_CANNOT_RESUME;\n        } else {\n            finalStatus = DownloaderService.STATUS_UNHANDLED_HTTP_CODE;\n        }\n        throw new StopRequest(finalStatus, \"http error \" + statusCode);\n    }\n\n    /**\n     * Add headers for this download to the HTTP request to allow for resume.\n     */\n    private void addRequestHeaders(InnerState innerState, HttpURLConnection request) {\n        if (innerState.mContinuingDownload) {\n            if (innerState.mHeaderETag != null) {\n                request.setRequestProperty(\"If-Match\", innerState.mHeaderETag);\n            }\n            request.setRequestProperty(\"Range\", \"bytes=\" + innerState.mBytesSoFar + \"-\");\n        }\n    }\n\n    /**\n     * Handle a 503 Service Unavailable status by processing the Retry-After\n     * header.\n     */\n    private void handleServiceUnavailable(State state, HttpURLConnection connection) throws StopRequest {\n        if (Constants.LOGVV) {\n            Log.v(Constants.TAG, \"got HTTP response code 503\");\n        }\n        state.mCountRetry = true;\n        String retryAfterValue = connection.getHeaderField(\"Retry-After\");\n        if (retryAfterValue != null) {\n            try {\n                if (Constants.LOGVV) {\n                    Log.v(Constants.TAG, \"Retry-After :\" + retryAfterValue);\n                }\n                state.mRetryAfter = Integer.parseInt(retryAfterValue);\n                if (state.mRetryAfter < 0) {\n                    state.mRetryAfter = 0;\n                } else {\n                    if (state.mRetryAfter < Constants.MIN_RETRY_AFTER) {\n                        state.mRetryAfter = Constants.MIN_RETRY_AFTER;\n                    } else if (state.mRetryAfter > Constants.MAX_RETRY_AFTER) {\n                        state.mRetryAfter = Constants.MAX_RETRY_AFTER;\n                    }\n                    state.mRetryAfter += Helpers.sRandom.nextInt(Constants.MIN_RETRY_AFTER + 1);\n                    state.mRetryAfter *= 1000;\n                }\n            } catch (NumberFormatException ex) {\n                // ignored - retryAfter stays 0 in this case.\n            }\n        }\n        throw new StopRequest(DownloaderService.STATUS_WAITING_TO_RETRY,\n                \"got 503 Service Unavailable, will retry later\");\n    }\n\n    /**\n     * Send the request to the server, handling any I/O exceptions.\n     */\n    private int sendRequest(State state, HttpURLConnection request)\n            throws StopRequest {\n        try {\n            return request.getResponseCode();\n        } catch (IllegalArgumentException ex) {\n            throw new StopRequest(DownloaderService.STATUS_HTTP_DATA_ERROR,\n                    \"while trying to execute request: \" + ex.toString(), ex);\n        } catch (IOException ex) {\n            logNetworkState();\n            throw new StopRequest(getFinalStatusForHttpError(state),\n                    \"while trying to execute request: \" + ex.toString(), ex);\n        }\n    }\n\n    private int getFinalStatusForHttpError(State state) {\n        if (mService.getNetworkAvailabilityState(mDB) != DownloaderService.NETWORK_OK) {\n            return DownloaderService.STATUS_WAITING_FOR_NETWORK;\n        } else if (mInfo.mNumFailed < Constants.MAX_RETRIES) {\n            state.mCountRetry = true;\n            return DownloaderService.STATUS_WAITING_TO_RETRY;\n        } else {\n            Log.w(Constants.TAG, \"reached max retries for \" + mInfo.mNumFailed);\n            return DownloaderService.STATUS_HTTP_DATA_ERROR;\n        }\n    }\n\n    /**\n     * Prepare the destination file to receive data. If the file already exists,\n     * we'll set up appropriately for resumption.\n     */\n    private void setupDestinationFile(State state, InnerState innerState)\n            throws StopRequest {\n        if (state.mFilename != null) { // only true if we've already run a\n                                       // thread for this download\n            if (!Helpers.isFilenameValid(state.mFilename)) {\n                // this should never happen\n                throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,\n                        \"found invalid internal destination filename\");\n            }\n            // We're resuming a download that got interrupted\n            File f = new File(state.mFilename);\n            if (f.exists()) {\n                long fileLength = f.length();\n                if (fileLength == 0) {\n                    // The download hadn't actually started, we can restart from\n                    // scratch\n                    f.delete();\n                    state.mFilename = null;\n                } else if (mInfo.mETag == null) {\n                    // This should've been caught upon failure\n                    f.delete();\n                    throw new StopRequest(DownloaderService.STATUS_CANNOT_RESUME,\n                            \"Trying to resume a download that can't be resumed\");\n                } else {\n                    // All right, we'll be able to resume this download\n                    try {\n                        state.mStream = new FileOutputStream(state.mFilename, true);\n                    } catch (FileNotFoundException exc) {\n                        throw new StopRequest(DownloaderService.STATUS_FILE_ERROR,\n                                \"while opening destination for resuming: \" + exc.toString(), exc);\n                    }\n                    innerState.mBytesSoFar = (int) fileLength;\n                    if (mInfo.mTotalBytes != -1) {\n                        innerState.mHeaderContentLength = Long.toString(mInfo.mTotalBytes);\n                    }\n                    innerState.mHeaderETag = mInfo.mETag;\n                    innerState.mContinuingDownload = true;\n                }\n            }\n        }\n\n        if (state.mStream != null) {\n            closeDestination(state);\n        }\n    }\n\n    /**\n     * Stores information about the completed download, and notifies the\n     * initiating application.\n     */\n    private void notifyDownloadCompleted(\n            int status, boolean countRetry, int retryAfter, int redirectCount, boolean gotData,\n            String filename) {\n        updateDownloadDatabase(\n                status, countRetry, retryAfter, redirectCount, gotData, filename);\n        if (DownloaderService.isStatusCompleted(status)) {\n            // TBD: send status update?\n        }\n    }\n\n    private void updateDownloadDatabase(\n            int status, boolean countRetry, int retryAfter, int redirectCount, boolean gotData,\n            String filename) {\n        mInfo.mStatus = status;\n        mInfo.mRetryAfter = retryAfter;\n        mInfo.mRedirectCount = redirectCount;\n        mInfo.mLastMod = System.currentTimeMillis();\n        if (!countRetry) {\n            mInfo.mNumFailed = 0;\n        } else if (gotData) {\n            mInfo.mNumFailed = 1;\n        } else {\n            mInfo.mNumFailed++;\n        }\n        mDB.updateDownload(mInfo);\n    }\n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/impl/DownloaderService.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader.impl;\n\nimport com.google.android.vending.expansion.downloader.Constants;\nimport com.google.android.vending.expansion.downloader.DownloadProgressInfo;\nimport com.google.android.vending.expansion.downloader.DownloaderServiceMarshaller;\nimport com.google.android.vending.expansion.downloader.Helpers;\nimport com.google.android.vending.expansion.downloader.IDownloaderClient;\nimport com.google.android.vending.expansion.downloader.IDownloaderService;\nimport com.google.android.vending.expansion.downloader.IStub;\nimport com.google.android.vending.licensing.AESObfuscator;\nimport com.google.android.vending.licensing.APKExpansionPolicy;\nimport com.google.android.vending.licensing.LicenseChecker;\nimport com.google.android.vending.licensing.LicenseCheckerCallback;\nimport com.google.android.vending.licensing.Policy;\n\nimport android.app.AlarmManager;\nimport android.app.PendingIntent;\nimport android.app.Service;\nimport android.content.BroadcastReceiver;\nimport android.content.Context;\nimport android.content.Intent;\nimport android.content.IntentFilter;\nimport android.content.pm.ApplicationInfo;\nimport android.content.pm.PackageInfo;\nimport android.content.pm.PackageManager.NameNotFoundException;\nimport android.net.ConnectivityManager;\nimport android.net.NetworkInfo;\nimport android.net.wifi.WifiManager;\nimport android.os.Handler;\nimport android.os.IBinder;\nimport android.os.Messenger;\nimport android.os.SystemClock;\nimport android.provider.Settings.Secure;\nimport android.telephony.TelephonyManager;\nimport android.util.Log;\n\nimport java.io.File;\n\n/**\n * Performs the background downloads requested by applications that use the\n * Downloads provider. This service does not run as a foreground task, so\n * Android may kill it off at will, but it will try to restart itself if it can.\n * Note that Android by default will kill off any process that has an open file\n * handle on the shared (SD Card) partition if the partition is unmounted.\n */\npublic abstract class DownloaderService extends CustomIntentService implements IDownloaderService {\n\n    public DownloaderService() {\n        super(\"LVLDownloadService\");\n    }\n\n    private static final String LOG_TAG = \"LVLDL\";\n\n    // the following NETWORK_* constants are used to indicates specific reasons\n    // for disallowing a\n    // download from using a network, since specific causes can require special\n    // handling\n\n    /**\n     * The network is usable for the given download.\n     */\n    public static final int NETWORK_OK = 1;\n\n    /**\n     * There is no network connectivity.\n     */\n    public static final int NETWORK_NO_CONNECTION = 2;\n\n    /**\n     * The download exceeds the maximum size for this network.\n     */\n    public static final int NETWORK_UNUSABLE_DUE_TO_SIZE = 3;\n\n    /**\n     * The download exceeds the recommended maximum size for this network, the\n     * user must confirm for this download to proceed without WiFi.\n     */\n    public static final int NETWORK_RECOMMENDED_UNUSABLE_DUE_TO_SIZE = 4;\n\n    /**\n     * The current connection is roaming, and the download can't proceed over a\n     * roaming connection.\n     */\n    public static final int NETWORK_CANNOT_USE_ROAMING = 5;\n\n    /**\n     * The app requesting the download specific that it can't use the current\n     * network connection.\n     */\n    public static final int NETWORK_TYPE_DISALLOWED_BY_REQUESTOR = 6;\n\n    /**\n     * For intents used to notify the user that a download exceeds a size\n     * threshold, if this extra is true, WiFi is required for this download\n     * size; otherwise, it is only recommended.\n     */\n    public static final String EXTRA_IS_WIFI_REQUIRED = \"isWifiRequired\";\n    public static final String EXTRA_FILE_NAME = \"downloadId\";\n\n    /**\n     * Used with DOWNLOAD_STATUS\n     */\n    public static final String EXTRA_STATUS_STATE = \"ESS\";\n    public static final String EXTRA_STATUS_TOTAL_SIZE = \"ETS\";\n    public static final String EXTRA_STATUS_CURRENT_FILE_SIZE = \"CFS\";\n    public static final String EXTRA_STATUS_TOTAL_PROGRESS = \"TFP\";\n    public static final String EXTRA_STATUS_CURRENT_PROGRESS = \"CFP\";\n\n    public static final String ACTION_DOWNLOADS_CHANGED = \"downloadsChanged\";\n\n    /**\n     * Broadcast intent action sent by the download manager when a download\n     * completes.\n     */\n    public final static String ACTION_DOWNLOAD_COMPLETE = \"lvldownloader.intent.action.DOWNLOAD_COMPLETE\";\n\n    /**\n     * Broadcast intent action sent by the download manager when download status\n     * changes.\n     */\n    public final static String ACTION_DOWNLOAD_STATUS = \"lvldownloader.intent.action.DOWNLOAD_STATUS\";\n\n    /*\n     * Lists the states that the download manager can set on a download to\n     * notify applications of the download progress. The codes follow the HTTP\n     * families:<br> 1xx: informational<br> 2xx: success<br> 3xx: redirects (not\n     * used by the download manager)<br> 4xx: client errors<br> 5xx: server\n     * errors\n     */\n\n    /**\n     * Returns whether the status is informational (i.e. 1xx).\n     */\n    public static boolean isStatusInformational(int status) {\n        return (status >= 100 && status < 200);\n    }\n\n    /**\n     * Returns whether the status is a success (i.e. 2xx).\n     */\n    public static boolean isStatusSuccess(int status) {\n        return (status >= 200 && status < 300);\n    }\n\n    /**\n     * Returns whether the status is an error (i.e. 4xx or 5xx).\n     */\n    public static boolean isStatusError(int status) {\n        return (status >= 400 && status < 600);\n    }\n\n    /**\n     * Returns whether the status is a client error (i.e. 4xx).\n     */\n    public static boolean isStatusClientError(int status) {\n        return (status >= 400 && status < 500);\n    }\n\n    /**\n     * Returns whether the status is a server error (i.e. 5xx).\n     */\n    public static boolean isStatusServerError(int status) {\n        return (status >= 500 && status < 600);\n    }\n\n    /**\n     * Returns whether the download has completed (either with success or\n     * error).\n     */\n    public static boolean isStatusCompleted(int status) {\n        return (status >= 200 && status < 300)\n                || (status >= 400 && status < 600);\n    }\n\n    /**\n     * This download hasn't stated yet\n     */\n    public static final int STATUS_PENDING = 190;\n\n    /**\n     * This download has started\n     */\n    public static final int STATUS_RUNNING = 192;\n\n    /**\n     * This download has been paused by the owning app.\n     */\n    public static final int STATUS_PAUSED_BY_APP = 193;\n\n    /**\n     * This download encountered some network error and is waiting before\n     * retrying the request.\n     */\n    public static final int STATUS_WAITING_TO_RETRY = 194;\n\n    /**\n     * This download is waiting for network connectivity to proceed.\n     */\n    public static final int STATUS_WAITING_FOR_NETWORK = 195;\n\n    /**\n     * This download is waiting for a Wi-Fi connection to proceed or for\n     * permission to download over cellular.\n     */\n    public static final int STATUS_QUEUED_FOR_WIFI_OR_CELLULAR_PERMISSION = 196;\n\n    /**\n     * This download is waiting for a Wi-Fi connection to proceed.\n     */\n    public static final int STATUS_QUEUED_FOR_WIFI = 197;\n\n    /**\n     * This download has successfully completed. Warning: there might be other\n     * status values that indicate success in the future. Use isSucccess() to\n     * capture the entire category.\n     *\n     * @hide\n     */\n    public static final int STATUS_SUCCESS = 200;\n\n    /**\n     * The requested URL is no longer available\n     */\n    public static final int STATUS_FORBIDDEN = 403;\n\n    /**\n     * The file was delivered incorrectly\n     */\n    public static final int STATUS_FILE_DELIVERED_INCORRECTLY = 487;\n\n    /**\n     * The requested destination file already exists.\n     */\n    public static final int STATUS_FILE_ALREADY_EXISTS_ERROR = 488;\n\n    /**\n     * Some possibly transient error occurred, but we can't resume the download.\n     */\n    public static final int STATUS_CANNOT_RESUME = 489;\n\n    /**\n     * This download was canceled\n     *\n     * @hide\n     */\n    public static final int STATUS_CANCELED = 490;\n\n    /**\n     * This download has completed with an error. Warning: there will be other\n     * status values that indicate errors in the future. Use isStatusError() to\n     * capture the entire category.\n     */\n    public static final int STATUS_UNKNOWN_ERROR = 491;\n\n    /**\n     * This download couldn't be completed because of a storage issue.\n     * Typically, that's because the filesystem is missing or full. Use the more\n     * specific {@link #STATUS_INSUFFICIENT_SPACE_ERROR} and\n     * {@link #STATUS_DEVICE_NOT_FOUND_ERROR} when appropriate.\n     *\n     * @hide\n     */\n    public static final int STATUS_FILE_ERROR = 492;\n\n    /**\n     * This download couldn't be completed because of an HTTP redirect response\n     * that the download manager couldn't handle.\n     *\n     * @hide\n     */\n    public static final int STATUS_UNHANDLED_REDIRECT = 493;\n\n    /**\n     * This download couldn't be completed because of an unspecified unhandled\n     * HTTP code.\n     *\n     * @hide\n     */\n    public static final int STATUS_UNHANDLED_HTTP_CODE = 494;\n\n    /**\n     * This download couldn't be completed because of an error receiving or\n     * processing data at the HTTP level.\n     *\n     * @hide\n     */\n    public static final int STATUS_HTTP_DATA_ERROR = 495;\n\n    /**\n     * This download couldn't be completed because of an HttpException while\n     * setting up the request.\n     *\n     * @hide\n     */\n    public static final int STATUS_HTTP_EXCEPTION = 496;\n\n    /**\n     * This download couldn't be completed because there were too many\n     * redirects.\n     *\n     * @hide\n     */\n    public static final int STATUS_TOO_MANY_REDIRECTS = 497;\n\n    /**\n     * This download couldn't be completed due to insufficient storage space.\n     * Typically, this is because the SD card is full.\n     *\n     * @hide\n     */\n    public static final int STATUS_INSUFFICIENT_SPACE_ERROR = 498;\n\n    /**\n     * This download couldn't be completed because no external storage device\n     * was found. Typically, this is because the SD card is not mounted.\n     *\n     * @hide\n     */\n    public static final int STATUS_DEVICE_NOT_FOUND_ERROR = 499;\n\n    /**\n     * This download is allowed to run.\n     *\n     * @hide\n     */\n    public static final int CONTROL_RUN = 0;\n\n    /**\n     * This download must pause at the first opportunity.\n     *\n     * @hide\n     */\n    public static final int CONTROL_PAUSED = 1;\n\n    /**\n     * This download is visible but only shows in the notifications while it's\n     * in progress.\n     *\n     * @hide\n     */\n    public static final int VISIBILITY_VISIBLE = 0;\n\n    /**\n     * This download is visible and shows in the notifications while in progress\n     * and after completion.\n     *\n     * @hide\n     */\n    public static final int VISIBILITY_VISIBLE_NOTIFY_COMPLETED = 1;\n\n    /**\n     * This download doesn't show in the UI or in the notifications.\n     *\n     * @hide\n     */\n    public static final int VISIBILITY_HIDDEN = 2;\n\n    /**\n     * Bit flag for setAllowedNetworkTypes corresponding to\n     * {@link ConnectivityManager#TYPE_MOBILE}.\n     */\n    public static final int NETWORK_MOBILE = 1 << 0;\n\n    /**\n     * Bit flag for setAllowedNetworkTypes corresponding to\n     * {@link ConnectivityManager#TYPE_WIFI}.\n     */\n    public static final int NETWORK_WIFI = 1 << 1;\n\n    private final static String TEMP_EXT = \".tmp\";\n\n    /**\n     * Service thread status\n     */\n    private static boolean sIsRunning;\n\n    @Override\n    public IBinder onBind(Intent paramIntent) {\n        Log.d(Constants.TAG, \"Service Bound\");\n        return this.mServiceMessenger.getBinder();\n    }\n\n    /**\n     * Network state.\n     */\n    private boolean mIsConnected;\n    private boolean mIsFailover;\n    private boolean mIsCellularConnection;\n    private boolean mIsRoaming;\n    private boolean mIsAtLeast3G;\n    private boolean mIsAtLeast4G;\n    private boolean mStateChanged;\n\n    /**\n     * Download state\n     */\n    private int mControl;\n    private int mStatus;\n\n    public boolean isWiFi() {\n        return mIsConnected && !mIsCellularConnection;\n    }\n\n    /**\n     * Bindings to important services\n     */\n    private ConnectivityManager mConnectivityManager;\n    private WifiManager mWifiManager;\n\n    /**\n     * Package we are downloading for (defaults to package of application)\n     */\n    private PackageInfo mPackageInfo;\n\n    /**\n     * Byte counts\n     */\n    long mBytesSoFar;\n    long mTotalLength;\n    int mFileCount;\n\n    /**\n     * Used for calculating time remaining and speed\n     */\n    long mBytesAtSample;\n    long mMillisecondsAtSample;\n    float mAverageDownloadSpeed;\n\n    /**\n     * Our binding to the network state broadcasts\n     */\n    private BroadcastReceiver mConnReceiver;\n    final private IStub mServiceStub = DownloaderServiceMarshaller.CreateStub(this);\n    final private Messenger mServiceMessenger = mServiceStub.getMessenger();\n    private Messenger mClientMessenger;\n    private DownloadNotification mNotification;\n    private PendingIntent mPendingIntent;\n    private PendingIntent mAlarmIntent;\n\n    /**\n     * Updates the network type based upon the type and subtype returned from\n     * the connectivity manager. Subtype is only used for cellular signals.\n     *\n     * @param type\n     * @param subType\n     */\n    private void updateNetworkType(int type, int subType) {\n        switch (type) {\n            case ConnectivityManager.TYPE_WIFI:\n            case ConnectivityManager.TYPE_ETHERNET:\n            case ConnectivityManager.TYPE_BLUETOOTH:\n                mIsCellularConnection = false;\n                mIsAtLeast3G = false;\n                mIsAtLeast4G = false;\n                break;\n            case ConnectivityManager.TYPE_WIMAX:\n                mIsCellularConnection = true;\n                mIsAtLeast3G = true;\n                mIsAtLeast4G = true;\n                break;\n            case ConnectivityManager.TYPE_MOBILE:\n                mIsCellularConnection = true;\n                switch (subType) {\n                    case TelephonyManager.NETWORK_TYPE_1xRTT:\n                    case TelephonyManager.NETWORK_TYPE_CDMA:\n                    case TelephonyManager.NETWORK_TYPE_EDGE:\n                    case TelephonyManager.NETWORK_TYPE_GPRS:\n                    case TelephonyManager.NETWORK_TYPE_IDEN:\n                        mIsAtLeast3G = false;\n                        mIsAtLeast4G = false;\n                        break;\n                    case TelephonyManager.NETWORK_TYPE_HSDPA:\n                    case TelephonyManager.NETWORK_TYPE_HSUPA:\n                    case TelephonyManager.NETWORK_TYPE_HSPA:\n                    case TelephonyManager.NETWORK_TYPE_EVDO_0:\n                    case TelephonyManager.NETWORK_TYPE_EVDO_A:\n                    case TelephonyManager.NETWORK_TYPE_UMTS:\n                        mIsAtLeast3G = true;\n                        mIsAtLeast4G = false;\n                        break;\n                    case TelephonyManager.NETWORK_TYPE_LTE: // 4G\n                    case TelephonyManager.NETWORK_TYPE_EHRPD: // 3G ++ interop\n                                                              // with 4G\n                    case TelephonyManager.NETWORK_TYPE_HSPAP: // 3G ++ but\n                                                              // marketed as\n                                                              // 4G\n                        mIsAtLeast3G = true;\n                        mIsAtLeast4G = true;\n                        break;\n                    default:\n                        mIsCellularConnection = false;\n                        mIsAtLeast3G = false;\n                        mIsAtLeast4G = false;\n                }\n        }\n    }\n\n    private void updateNetworkState(NetworkInfo info) {\n        boolean isConnected = mIsConnected;\n        boolean isFailover = mIsFailover;\n        boolean isCellularConnection = mIsCellularConnection;\n        boolean isRoaming = mIsRoaming;\n        boolean isAtLeast3G = mIsAtLeast3G;\n        if (null != info) {\n            mIsRoaming = info.isRoaming();\n            mIsFailover = info.isFailover();\n            mIsConnected = info.isConnected();\n            updateNetworkType(info.getType(), info.getSubtype());\n        } else {\n            mIsRoaming = false;\n            mIsFailover = false;\n            mIsConnected = false;\n            updateNetworkType(-1, -1);\n        }\n        mStateChanged = (mStateChanged || isConnected != mIsConnected\n                || isFailover != mIsFailover\n                || isCellularConnection != mIsCellularConnection\n                || isRoaming != mIsRoaming || isAtLeast3G != mIsAtLeast3G);\n        if (Constants.LOGVV) {\n            if (mStateChanged) {\n                Log.v(LOG_TAG, \"Network state changed: \");\n                Log.v(LOG_TAG, \"Starting State: \" +\n                        (isConnected ? \"Connected \" : \"Not Connected \") +\n                        (isCellularConnection ? \"Cellular \" : \"WiFi \") +\n                        (isRoaming ? \"Roaming \" : \"Local \") +\n                        (isAtLeast3G ? \"3G+ \" : \"<3G \"));\n                Log.v(LOG_TAG, \"Ending State: \" +\n                        (mIsConnected ? \"Connected \" : \"Not Connected \") +\n                        (mIsCellularConnection ? \"Cellular \" : \"WiFi \") +\n                        (mIsRoaming ? \"Roaming \" : \"Local \") +\n                        (mIsAtLeast3G ? \"3G+ \" : \"<3G \"));\n\n                if (isServiceRunning()) {\n                    if (mIsRoaming) {\n                        mStatus = STATUS_WAITING_FOR_NETWORK;\n                        mControl = CONTROL_PAUSED;\n                    } else if (mIsCellularConnection) {\n                        DownloadsDB db = DownloadsDB.getDB(this);\n                        int flags = db.getFlags();\n                        if (0 == (flags & FLAGS_DOWNLOAD_OVER_CELLULAR)) {\n                            mStatus = STATUS_QUEUED_FOR_WIFI;\n                            mControl = CONTROL_PAUSED;\n                        }\n                    }\n                }\n\n            }\n        }\n    }\n\n    /**\n     * Polls the network state, setting the flags appropriately.\n     */\n    void pollNetworkState() {\n        if (null == mConnectivityManager) {\n            mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);\n        }\n        if (null == mWifiManager) {\n            mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);\n        }\n        if (mConnectivityManager == null) {\n            Log.w(Constants.TAG,\n                    \"couldn't get connectivity manager to poll network state\");\n        } else {\n            NetworkInfo activeInfo = mConnectivityManager\n                    .getActiveNetworkInfo();\n            updateNetworkState(activeInfo);\n        }\n    }\n\n    public static final int NO_DOWNLOAD_REQUIRED = 0;\n    public static final int LVL_CHECK_REQUIRED = 1;\n    public static final int DOWNLOAD_REQUIRED = 2;\n\n    public static final String EXTRA_PACKAGE_NAME = \"EPN\";\n    public static final String EXTRA_PENDING_INTENT = \"EPI\";\n    public static final String EXTRA_MESSAGE_HANDLER = \"EMH\";\n\n    /**\n     * Returns true if the LVL check is required\n     *\n     * @param db a downloads DB synchronized with the latest state\n     * @param pi the package info for the project\n     * @return returns true if the filenames need to be returned\n     */\n    private static boolean isLVLCheckRequired(DownloadsDB db, PackageInfo pi) {\n        // we need to update the LVL check and get a successful status to\n        // proceed\n        if (db.mVersionCode != pi.versionCode) {\n            return true;\n        }\n        return false;\n    }\n\n    /**\n     * Careful! Only use this internally.\n     *\n     * @return whether we think the service is running\n     */\n    private static synchronized boolean isServiceRunning() {\n        return sIsRunning;\n    }\n\n    private static synchronized void setServiceRunning(boolean isRunning) {\n        sIsRunning = isRunning;\n    }\n\n    public static int startDownloadServiceIfRequired(Context context,\n            Intent intent, Class<?> serviceClass) throws NameNotFoundException {\n        final PendingIntent pendingIntent = (PendingIntent) intent\n                .getParcelableExtra(EXTRA_PENDING_INTENT);\n        return startDownloadServiceIfRequired(context, pendingIntent,\n                serviceClass);\n    }\n\n    public static int startDownloadServiceIfRequired(Context context,\n            PendingIntent pendingIntent, Class<?> serviceClass)\n            throws NameNotFoundException\n    {\n        String packageName = context.getPackageName();\n        String className = serviceClass.getName();\n\n        return startDownloadServiceIfRequired(context, pendingIntent,\n                packageName, className);\n    }\n\n    /**\n     * Starts the download if necessary. This function starts a flow that does `\n     * many things. 1) Checks to see if the APK version has been checked and the\n     * metadata database updated 2) If the APK version does not match, checks\n     * the new LVL status to see if a new download is required 3) If the APK\n     * version does match, then checks to see if the download(s) have been\n     * completed 4) If the downloads have been completed, returns\n     * NO_DOWNLOAD_REQUIRED The idea is that this can be called during the\n     * startup of an application to quickly ascertain if the application needs\n     * to wait to hear about any updated APK expansion files. Note that this\n     * does mean that the application MUST be run for the first time with a\n     * network connection, even if Market delivers all of the files.\n     *\n     * @param context\n     * @param pendingIntent\n     * @return true if the app should wait for more guidance from the\n     *         downloader, false if the app can continue\n     * @throws NameNotFoundException\n     */\n    public static int startDownloadServiceIfRequired(Context context,\n            PendingIntent pendingIntent, String classPackage, String className)\n            throws NameNotFoundException {\n        // first: do we need to do an LVL update?\n        // we begin by getting our APK version from the package manager\n        final PackageInfo pi = context.getPackageManager().getPackageInfo(\n                context.getPackageName(), 0);\n\n        int status = NO_DOWNLOAD_REQUIRED;\n\n        // the database automatically reads the metadata for version code\n        // and download status when the instance is created\n        DownloadsDB db = DownloadsDB.getDB(context);\n\n        // we need to update the LVL check and get a successful status to\n        // proceed\n        if (isLVLCheckRequired(db, pi)) {\n            status = LVL_CHECK_REQUIRED;\n        }\n        // we don't have to update LVL. do we still have a download to start?\n        if (db.mStatus == 0) {\n            DownloadInfo[] infos = db.getDownloads();\n            if (null != infos) {\n                for (DownloadInfo info : infos) {\n                    if (!Helpers.doesFileExist(context, info.mFileName, info.mTotalBytes, true)) {\n                        status = DOWNLOAD_REQUIRED;\n                        db.updateStatus(-1);\n                        break;\n                    }\n                }\n            }\n        } else {\n            status = DOWNLOAD_REQUIRED;\n        }\n        switch (status) {\n            case DOWNLOAD_REQUIRED:\n            case LVL_CHECK_REQUIRED:\n                Intent fileIntent = new Intent();\n                fileIntent.setClassName(classPackage, className);\n                fileIntent.putExtra(EXTRA_PENDING_INTENT, pendingIntent);\n                context.startService(fileIntent);\n                break;\n        }\n        return status;\n    }\n\n    @Override\n    public void requestAbortDownload() {\n        mControl = CONTROL_PAUSED;\n        mStatus = STATUS_CANCELED;\n    }\n\n    @Override\n    public void requestPauseDownload() {\n        mControl = CONTROL_PAUSED;\n        mStatus = STATUS_PAUSED_BY_APP;\n    }\n\n    @Override\n    public void setDownloadFlags(int flags) {\n        DownloadsDB.getDB(this).updateFlags(flags);\n    }\n\n    @Override\n    public void requestContinueDownload() {\n        if (mControl == CONTROL_PAUSED) {\n            mControl = CONTROL_RUN;\n        }\n        Intent fileIntent = new Intent(this, this.getClass());\n        fileIntent.putExtra(EXTRA_PENDING_INTENT, mPendingIntent);\n        this.startService(fileIntent);\n    }\n\n    public abstract String getPublicKey();\n\n    public abstract byte[] getSALT();\n\n    public abstract String getAlarmReceiverClassName();\n\n    private class LVLRunnable implements Runnable {\n        LVLRunnable(Context context, PendingIntent intent) {\n            mContext = context;\n            mPendingIntent = intent;\n        }\n\n        final Context mContext;\n\n        @Override\n        public void run() {\n            setServiceRunning(true);\n            mNotification.onDownloadStateChanged(IDownloaderClient.STATE_FETCHING_URL);\n            String deviceId = Secure.getString(mContext.getContentResolver(),\n                    Secure.ANDROID_ID);\n\n            final APKExpansionPolicy aep = new APKExpansionPolicy(mContext,\n                    new AESObfuscator(getSALT(), mContext.getPackageName(), deviceId));\n\n            // reset our policy back to the start of the world to force a\n            // re-check\n            aep.resetPolicy();\n\n            // let's try and get the OBB file from LVL first\n            // Construct the LicenseChecker with a Policy.\n            final LicenseChecker checker = new LicenseChecker(mContext, aep,\n                    getPublicKey() // Your public licensing key.\n            );\n            checker.checkAccess(new LicenseCheckerCallback() {\n\n                @Override\n                public void allow(int reason) {\n                    try {\n                        int count = aep.getExpansionURLCount();\n                        DownloadsDB db = DownloadsDB.getDB(mContext);\n                        int status = 0;\n                        if (count != 0) {\n                            for (int i = 0; i < count; i++) {\n                                String currentFileName = aep\n                                        .getExpansionFileName(i);\n                                if (null != currentFileName) {\n                                    DownloadInfo di = new DownloadInfo(i,\n                                            currentFileName, mContext.getPackageName());\n\n                                    long fileSize = aep.getExpansionFileSize(i);\n                                    if (handleFileUpdated(db, i, currentFileName,\n                                            fileSize)) {\n                                        status |= -1;\n                                        di.resetDownload();\n                                        di.mUri = aep.getExpansionURL(i);\n                                        di.mTotalBytes = fileSize;\n                                        di.mStatus = status;\n                                        db.updateDownload(di);\n                                    } else {\n                                        // we need to read the download\n                                        // information\n                                        // from\n                                        // the database\n                                        DownloadInfo dbdi = db\n                                                .getDownloadInfoByFileName(di.mFileName);\n                                        if (null == dbdi) {\n                                            // the file exists already and is\n                                            // the\n                                            // correct size\n                                            // was delivered by Market or\n                                            // through\n                                            // another mechanism\n                                            Log.d(LOG_TAG, \"file \" + di.mFileName\n                                                    + \" found. Not downloading.\");\n                                            di.mStatus = STATUS_SUCCESS;\n                                            di.mTotalBytes = fileSize;\n                                            di.mCurrentBytes = fileSize;\n                                            di.mUri = aep.getExpansionURL(i);\n                                            db.updateDownload(di);\n                                        } else if (dbdi.mStatus != STATUS_SUCCESS) {\n                                            // we just update the URL\n                                            dbdi.mUri = aep.getExpansionURL(i);\n                                            db.updateDownload(dbdi);\n                                            status |= -1;\n                                        }\n                                    }\n                                }\n                            }\n                        }\n                        // first: do we need to do an LVL update?\n                        // we begin by getting our APK version from the package\n                        // manager\n                        PackageInfo pi;\n                        try {\n                            pi = mContext.getPackageManager().getPackageInfo(\n                                    mContext.getPackageName(), 0);\n                            db.updateMetadata(pi.versionCode, status);\n                            Class<?> serviceClass = DownloaderService.this.getClass();\n                            switch (startDownloadServiceIfRequired(mContext, mPendingIntent,\n                                    serviceClass)) {\n                                case NO_DOWNLOAD_REQUIRED:\n                                    mNotification\n                                            .onDownloadStateChanged(IDownloaderClient.STATE_COMPLETED);\n                                    break;\n                                case LVL_CHECK_REQUIRED:\n                                    // DANGER WILL ROBINSON!\n                                    Log.e(LOG_TAG, \"In LVL checking loop!\");\n                                    mNotification\n                                            .onDownloadStateChanged(IDownloaderClient.STATE_FAILED_UNLICENSED);\n                                    throw new RuntimeException(\n                                            \"Error with LVL checking and database integrity\");\n                                case DOWNLOAD_REQUIRED:\n                                    // do nothing. the download will notify the\n                                    // application\n                                    // when things are done\n                                    break;\n                            }\n                        } catch (NameNotFoundException e1) {\n                            e1.printStackTrace();\n                            throw new RuntimeException(\n                                    \"Error with getting information from package name\");\n                        }\n                    } finally {\n                        setServiceRunning(false);\n                    }\n                }\n\n                @Override\n                public void dontAllow(int reason) {\n                    try\n                    {\n                        switch (reason) {\n                            case Policy.NOT_LICENSED:\n                                mNotification\n                                        .onDownloadStateChanged(IDownloaderClient.STATE_FAILED_UNLICENSED);\n                                break;\n                            case Policy.RETRY:\n                                mNotification\n                                        .onDownloadStateChanged(IDownloaderClient.STATE_FAILED_FETCHING_URL);\n                                break;\n                        }\n                    } finally {\n                        setServiceRunning(false);\n                    }\n\n                }\n\n                @Override\n                public void applicationError(int errorCode) {\n                    try {\n                        mNotification\n                                .onDownloadStateChanged(IDownloaderClient.STATE_FAILED_FETCHING_URL);\n                    } finally {\n                        setServiceRunning(false);\n                    }\n                }\n\n            });\n\n        }\n\n    };\n\n    /**\n     * Updates the LVL information from the server.\n     *\n     * @param context\n     */\n    public void updateLVL(final Context context) {\n        Context c = context.getApplicationContext();\n        Handler h = new Handler(c.getMainLooper());\n        h.post(new LVLRunnable(c, mPendingIntent));\n    }\n\n    /**\n     * The APK has been updated and a filename has been sent down from the\n     * Market call. If the file has the same name as the previous file, we do\n     * nothing as the file is guaranteed to be the same. If the file does not\n     * have the same name, we download it if it hasn't already been delivered by\n     * Market.\n     *\n     * @param index the index of the file from market (0 = main, 1 = patch)\n     * @param filename the name of the new file\n     * @param fileSize the size of the new file\n     * @return\n     */\n    public boolean handleFileUpdated(DownloadsDB db, int index,\n            String filename, long fileSize) {\n        DownloadInfo di = db.getDownloadInfoByFileName(filename);\n        if (null != di) {\n            String oldFile = di.mFileName;\n            // cleanup\n            if (null != oldFile) {\n                if (filename.equals(oldFile)) {\n                    return false;\n                }\n\n                // remove partially downloaded file if it is there\n                String deleteFile = Helpers.generateSaveFileName(this, oldFile);\n                File f = new File(deleteFile);\n                if (f.exists())\n                    f.delete();\n            }\n        }\n        return !Helpers.doesFileExist(this, filename, fileSize, true);\n    }\n\n    private void scheduleAlarm(long wakeUp) {\n        AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);\n        if (alarms == null) {\n            Log.e(Constants.TAG, \"couldn't get alarm manager\");\n            return;\n        }\n\n        if (Constants.LOGV) {\n            Log.v(Constants.TAG, \"scheduling retry in \" + wakeUp + \"ms\");\n        }\n\n        String className = getAlarmReceiverClassName();\n        Intent intent = new Intent(Constants.ACTION_RETRY);\n        intent.putExtra(EXTRA_PENDING_INTENT, mPendingIntent);\n        intent.setClassName(this.getPackageName(),\n                className);\n        mAlarmIntent = PendingIntent.getBroadcast(this, 0, intent,\n                PendingIntent.FLAG_ONE_SHOT);\n        alarms.set(\n                AlarmManager.RTC_WAKEUP,\n                System.currentTimeMillis() + wakeUp, mAlarmIntent\n                );\n    }\n\n    private void cancelAlarms() {\n        if (null != mAlarmIntent) {\n            AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);\n            if (alarms == null) {\n                Log.e(Constants.TAG, \"couldn't get alarm manager\");\n                return;\n            }\n            alarms.cancel(mAlarmIntent);\n            mAlarmIntent = null;\n        }\n    }\n\n    /**\n     * We use this to track network state, such as when WiFi, Cellular, etc. is\n     * enabled when downloads are paused or in progress.\n     */\n    private class InnerBroadcastReceiver extends BroadcastReceiver {\n        final Service mService;\n\n        InnerBroadcastReceiver(Service service) {\n            mService = service;\n        }\n\n        @Override\n        public void onReceive(Context context, Intent intent) {\n            pollNetworkState();\n            if (mStateChanged\n                    && !isServiceRunning()) {\n                Log.d(Constants.TAG, \"InnerBroadcastReceiver Called\");\n                Intent fileIntent = new Intent(context, mService.getClass());\n                fileIntent.putExtra(EXTRA_PENDING_INTENT, mPendingIntent);\n                // send a new intent to the service\n                context.startService(fileIntent);\n            }\n        }\n    };\n\n    /**\n     * This is the main thread for the Downloader. This thread is responsible\n     * for queuing up downloads and other goodness.\n     */\n    @Override\n    protected void onHandleIntent(Intent intent) {\n        setServiceRunning(true);\n        try {\n            // the database automatically reads the metadata for version code\n            // and download status when the instance is created\n            DownloadsDB db = DownloadsDB.getDB(this);\n            final PendingIntent pendingIntent = (PendingIntent) intent\n                    .getParcelableExtra(EXTRA_PENDING_INTENT);\n\n            if (null != pendingIntent)\n            {\n                mNotification.setClientIntent(pendingIntent);\n                mPendingIntent = pendingIntent;\n            } else if (null != mPendingIntent) {\n                mNotification.setClientIntent(mPendingIntent);\n            } else {\n                Log.e(LOG_TAG, \"Downloader started in bad state without notification intent.\");\n                return;\n            }\n\n            // when the LVL check completes, a successful response will update\n            // the service\n            if (isLVLCheckRequired(db, mPackageInfo)) {\n                updateLVL(this);\n                return;\n            }\n\n            // get each download\n            DownloadInfo[] infos = db.getDownloads();\n            mBytesSoFar = 0;\n            mTotalLength = 0;\n            mFileCount = infos.length;\n            for (DownloadInfo info : infos) {\n                // We do an (simple) integrity check on each file, just to make\n                // sure\n                if (info.mStatus == STATUS_SUCCESS) {\n                    // verify that the file matches the state\n                    if (!Helpers.doesFileExist(this, info.mFileName, info.mTotalBytes, true)) {\n                        info.mStatus = 0;\n                        info.mCurrentBytes = 0;\n                    }\n                }\n                // get aggregate data\n                mTotalLength += info.mTotalBytes;\n                mBytesSoFar += info.mCurrentBytes;\n            }\n\n            // loop through all downloads and fetch them\n            pollNetworkState();\n            if (null == mConnReceiver) {\n\n                /**\n                 * We use this to track network state, such as when WiFi,\n                 * Cellular, etc. is enabled when downloads are paused or in\n                 * progress.\n                 */\n                mConnReceiver = new InnerBroadcastReceiver(this);\n                IntentFilter intentFilter = new IntentFilter(\n                        ConnectivityManager.CONNECTIVITY_ACTION);\n                intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);\n                registerReceiver(mConnReceiver, intentFilter);\n            }\n\n            for (DownloadInfo info : infos) {\n                long startingCount = info.mCurrentBytes;\n\n                if (info.mStatus != STATUS_SUCCESS) {\n                    DownloadThread dt = new DownloadThread(info, this, mNotification);\n                    cancelAlarms();\n                    scheduleAlarm(Constants.ACTIVE_THREAD_WATCHDOG);\n                    dt.run();\n                    cancelAlarms();\n                }\n                db.updateFromDb(info);\n                boolean setWakeWatchdog = false;\n                int notifyStatus;\n                switch (info.mStatus) {\n                    case STATUS_FORBIDDEN:\n                        // the URL is out of date\n                        updateLVL(this);\n                        return;\n                    case STATUS_SUCCESS:\n                        mBytesSoFar += info.mCurrentBytes - startingCount;\n                        db.updateMetadata(mPackageInfo.versionCode, 0);\n                        continue;\n                    case STATUS_FILE_DELIVERED_INCORRECTLY:\n                        // we may be on a network that is returning us a web\n                        // page on redirect\n                        notifyStatus = IDownloaderClient.STATE_PAUSED_NETWORK_SETUP_FAILURE;\n                        info.mCurrentBytes = 0;\n                        db.updateDownload(info);\n                        setWakeWatchdog = true;\n                        break;\n                    case STATUS_PAUSED_BY_APP:\n                        notifyStatus = IDownloaderClient.STATE_PAUSED_BY_REQUEST;\n                        break;\n                    case STATUS_WAITING_FOR_NETWORK:\n                    case STATUS_WAITING_TO_RETRY:\n                        notifyStatus = IDownloaderClient.STATE_PAUSED_NETWORK_UNAVAILABLE;\n                        setWakeWatchdog = true;\n                        break;\n                    case STATUS_QUEUED_FOR_WIFI_OR_CELLULAR_PERMISSION:\n                    case STATUS_QUEUED_FOR_WIFI:\n                        // look for more detail here\n                        if (null != mWifiManager) {\n                            if (!mWifiManager.isWifiEnabled()) {\n                                notifyStatus = IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION;\n                                setWakeWatchdog = true;\n                                break;\n                            }\n                        }\n                        notifyStatus = IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION;\n                        setWakeWatchdog = true;\n                        break;\n                    case STATUS_CANCELED:\n                        notifyStatus = IDownloaderClient.STATE_FAILED_CANCELED;\n                        setWakeWatchdog = true;\n                        break;\n\n                    case STATUS_INSUFFICIENT_SPACE_ERROR:\n                        notifyStatus = IDownloaderClient.STATE_FAILED_SDCARD_FULL;\n                        setWakeWatchdog = true;\n                        break;\n\n                    case STATUS_DEVICE_NOT_FOUND_ERROR:\n                        notifyStatus = IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE;\n                        setWakeWatchdog = true;\n                        break;\n\n                    default:\n                        notifyStatus = IDownloaderClient.STATE_FAILED;\n                        break;\n                }\n                if (setWakeWatchdog) {\n                    scheduleAlarm(Constants.WATCHDOG_WAKE_TIMER);\n                } else {\n                    cancelAlarms();\n                }\n                // failure or pause state\n                mNotification.onDownloadStateChanged(notifyStatus);\n                return;\n            }\n\n            // all downloads complete\n            mNotification.onDownloadStateChanged(IDownloaderClient.STATE_COMPLETED);\n        } finally {\n            setServiceRunning(false);\n        }\n    }\n\n    @Override\n    public void onDestroy() {\n        if (null != mConnReceiver) {\n            unregisterReceiver(mConnReceiver);\n            mConnReceiver = null;\n        }\n        mServiceStub.disconnect(this);\n        super.onDestroy();\n    }\n\n    public int getNetworkAvailabilityState(DownloadsDB db) {\n        if (mIsConnected) {\n            if (!mIsCellularConnection)\n                return NETWORK_OK;\n            int flags = db.mFlags;\n            if (mIsRoaming)\n                return NETWORK_CANNOT_USE_ROAMING;\n            if (0 != (flags & FLAGS_DOWNLOAD_OVER_CELLULAR)) {\n                return NETWORK_OK;\n            } else {\n                return NETWORK_TYPE_DISALLOWED_BY_REQUESTOR;\n            }\n        }\n        return NETWORK_NO_CONNECTION;\n    }\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        try {\n            mPackageInfo = getPackageManager().getPackageInfo(\n                    getPackageName(), 0);\n            ApplicationInfo ai = getApplicationInfo();\n            CharSequence applicationLabel = getPackageManager().getApplicationLabel(ai);\n            mNotification = new DownloadNotification(this, applicationLabel);\n\n        } catch (NameNotFoundException e) {\n            e.printStackTrace();\n        }\n    }\n\n    /**\n     * Exception thrown from methods called by generateSaveFile() for any fatal\n     * error.\n     */\n    public static class GenerateSaveFileError extends Exception {\n        private static final long serialVersionUID = 3465966015408936540L;\n        int mStatus;\n        String mMessage;\n\n        public GenerateSaveFileError(int status, String message) {\n            mStatus = status;\n            mMessage = message;\n        }\n    }\n\n    /**\n     * Returns the filename (where the file should be saved) from info about a\n     * download\n     */\n    public String generateTempSaveFileName(String fileName) {\n        String path = Helpers.getSaveFilePath(this)\n                + File.separator + fileName + TEMP_EXT;\n        return path;\n    }\n\n    /**\n     * Creates a filename (where the file should be saved) from info about a\n     * download.\n     */\n    public String generateSaveFile(String filename, long filesize)\n            throws GenerateSaveFileError {\n        String path = generateTempSaveFileName(filename);\n        File expPath = new File(path);\n        if (!Helpers.isExternalMediaMounted()) {\n            Log.d(Constants.TAG, \"External media not mounted: \" + path);\n            throw new GenerateSaveFileError(STATUS_DEVICE_NOT_FOUND_ERROR,\n                    \"external media is not yet mounted\");\n\n        }\n        if (expPath.exists()) {\n            Log.d(Constants.TAG, \"File already exists: \" + path);\n            throw new GenerateSaveFileError(STATUS_FILE_ALREADY_EXISTS_ERROR,\n                    \"requested destination file already exists\");\n        }\n        if (Helpers.getAvailableBytes(Helpers.getFilesystemRoot(path)) < filesize) {\n            throw new GenerateSaveFileError(STATUS_INSUFFICIENT_SPACE_ERROR,\n                    \"insufficient space on external storage\");\n        }\n        return path;\n    }\n\n    /**\n     * @return a non-localized string appropriate for logging corresponding to\n     *         one of the NETWORK_* constants.\n     */\n    public String getLogMessageForNetworkError(int networkError) {\n        switch (networkError) {\n            case NETWORK_RECOMMENDED_UNUSABLE_DUE_TO_SIZE:\n                return \"download size exceeds recommended limit for mobile network\";\n\n            case NETWORK_UNUSABLE_DUE_TO_SIZE:\n                return \"download size exceeds limit for mobile network\";\n\n            case NETWORK_NO_CONNECTION:\n                return \"no network connection available\";\n\n            case NETWORK_CANNOT_USE_ROAMING:\n                return \"download cannot use the current network connection because it is roaming\";\n\n            case NETWORK_TYPE_DISALLOWED_BY_REQUESTOR:\n                return \"download was requested to not use the current network type\";\n\n            default:\n                return \"unknown error with network connectivity\";\n        }\n    }\n\n    public int getControl() {\n        return mControl;\n    }\n\n    public int getStatus() {\n        return mStatus;\n    }\n\n    /**\n     * Calculating a moving average for the speed so we don't get jumpy\n     * calculations for time etc.\n     */\n    static private final float SMOOTHING_FACTOR = 0.005f;\n\n    public void notifyUpdateBytes(long totalBytesSoFar) {\n        long timeRemaining;\n        long currentTime = SystemClock.uptimeMillis();\n        if (0 != mMillisecondsAtSample) {\n            // we have a sample.\n            long timePassed = currentTime - mMillisecondsAtSample;\n            long bytesInSample = totalBytesSoFar - mBytesAtSample;\n            float currentSpeedSample = (float) bytesInSample / (float) timePassed;\n            if (0 != mAverageDownloadSpeed) {\n                mAverageDownloadSpeed = SMOOTHING_FACTOR * currentSpeedSample\n                        + (1 - SMOOTHING_FACTOR) * mAverageDownloadSpeed;\n            } else {\n                mAverageDownloadSpeed = currentSpeedSample;\n            }\n            timeRemaining = (long) ((mTotalLength - totalBytesSoFar) / mAverageDownloadSpeed);\n        } else {\n            timeRemaining = -1;\n        }\n        mMillisecondsAtSample = currentTime;\n        mBytesAtSample = totalBytesSoFar;\n        mNotification.onDownloadProgress(\n                new DownloadProgressInfo(mTotalLength,\n                        totalBytesSoFar,\n                        timeRemaining,\n                        mAverageDownloadSpeed)\n                );\n\n    }\n\n    @Override\n    protected boolean shouldStop() {\n        // the database automatically reads the metadata for version code\n        // and download status when the instance is created\n        DownloadsDB db = DownloadsDB.getDB(this);\n        if (db.mStatus == 0) {\n            return true;\n        }\n        return false;\n    }\n\n    @Override\n    public void requestDownloadStatus() {\n        mNotification.resendState();\n    }\n\n    @Override\n    public void onClientUpdated(Messenger clientMessenger) {\n        this.mClientMessenger = clientMessenger;\n        mNotification.setMessenger(mClientMessenger);\n    }\n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/impl/DownloadsDB.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader.impl;\n\nimport android.content.ContentValues;\nimport android.content.Context;\nimport android.database.Cursor;\nimport android.database.sqlite.SQLiteDatabase;\nimport android.database.sqlite.SQLiteDoneException;\nimport android.database.sqlite.SQLiteOpenHelper;\nimport android.database.sqlite.SQLiteStatement;\nimport android.provider.BaseColumns;\nimport android.util.Log;\n\npublic class DownloadsDB {\n    private static final String DATABASE_NAME = \"DownloadsDB\";\n    private static final int DATABASE_VERSION = 7;\n    public static final String LOG_TAG = DownloadsDB.class.getName();\n    final SQLiteOpenHelper mHelper;\n    SQLiteStatement mGetDownloadByIndex;\n    SQLiteStatement mUpdateCurrentBytes;\n    private static DownloadsDB mDownloadsDB;\n    long mMetadataRowID = -1;\n    int mVersionCode = -1;\n    int mStatus = -1;\n    int mFlags;\n\n    static public synchronized DownloadsDB getDB(Context paramContext) {\n        if (null == mDownloadsDB) {\n            return new DownloadsDB(paramContext);\n        }\n        return mDownloadsDB;\n    }\n\n    private SQLiteStatement getDownloadByIndexStatement() {\n        if (null == mGetDownloadByIndex) {\n            mGetDownloadByIndex = mHelper.getReadableDatabase().compileStatement(\n                    \"SELECT \" + BaseColumns._ID + \" FROM \"\n                            + DownloadColumns.TABLE_NAME + \" WHERE \"\n                            + DownloadColumns.INDEX + \" = ?\");\n        }\n        return mGetDownloadByIndex;\n    }\n\n    private SQLiteStatement getUpdateCurrentBytesStatement() {\n        if (null == mUpdateCurrentBytes) {\n            mUpdateCurrentBytes = mHelper.getReadableDatabase().compileStatement(\n                    \"UPDATE \" + DownloadColumns.TABLE_NAME + \" SET \" + DownloadColumns.CURRENTBYTES\n                            + \" = ?\" +\n                            \" WHERE \" + DownloadColumns.INDEX + \" = ?\");\n        }\n        return mUpdateCurrentBytes;\n    }\n\n    private DownloadsDB(Context paramContext) {\n        this.mHelper = new DownloadsContentDBHelper(paramContext);\n        final SQLiteDatabase sqldb = mHelper.getReadableDatabase();\n        // Query for the version code, the row ID of the metadata (for future\n        // updating) the status and the flags\n        Cursor cur = sqldb.rawQuery(\"SELECT \" +\n                MetadataColumns.APKVERSION + \",\" +\n                BaseColumns._ID + \",\" +\n                MetadataColumns.DOWNLOAD_STATUS + \",\" +\n                MetadataColumns.FLAGS +\n                \" FROM \"\n                + MetadataColumns.TABLE_NAME + \" LIMIT 1\", null);\n        if (null != cur && cur.moveToFirst()) {\n            mVersionCode = cur.getInt(0);\n            mMetadataRowID = cur.getLong(1);\n            mStatus = cur.getInt(2);\n            mFlags = cur.getInt(3);\n            cur.close();\n        }\n        mDownloadsDB = this;\n    }\n\n    protected DownloadInfo getDownloadInfoByFileName(String fileName) {\n        final SQLiteDatabase sqldb = mHelper.getReadableDatabase();\n        Cursor itemcur = null;\n        try {\n            itemcur = sqldb.query(DownloadColumns.TABLE_NAME, DC_PROJECTION,\n                    DownloadColumns.FILENAME + \" = ?\",\n                    new String[] {\n                        fileName\n                    }, null, null, null);\n            if (null != itemcur && itemcur.moveToFirst()) {\n                return getDownloadInfoFromCursor(itemcur);\n            }\n        } finally {\n            if (null != itemcur)\n                itemcur.close();\n        }\n        return null;\n    }\n\n    public long getIDForDownloadInfo(final DownloadInfo di) {\n        return getIDByIndex(di.mIndex);\n    }\n\n    public long getIDByIndex(int index) {\n        SQLiteStatement downloadByIndex = getDownloadByIndexStatement();\n        downloadByIndex.clearBindings();\n        downloadByIndex.bindLong(1, index);\n        try {\n            return downloadByIndex.simpleQueryForLong();\n        } catch (SQLiteDoneException e) {\n            return -1;\n        }\n    }\n\n    public void updateDownloadCurrentBytes(final DownloadInfo di) {\n        SQLiteStatement downloadCurrentBytes = getUpdateCurrentBytesStatement();\n        downloadCurrentBytes.clearBindings();\n        downloadCurrentBytes.bindLong(1, di.mCurrentBytes);\n        downloadCurrentBytes.bindLong(2, di.mIndex);\n        downloadCurrentBytes.execute();\n    }\n\n    public void close() {\n        this.mHelper.close();\n    }\n\n    protected static class DownloadsContentDBHelper extends SQLiteOpenHelper {\n        DownloadsContentDBHelper(Context paramContext) {\n            super(paramContext, DATABASE_NAME, null, DATABASE_VERSION);\n        }\n\n        private String createTableQueryFromArray(String paramString,\n                String[][] paramArrayOfString) {\n            StringBuilder localStringBuilder = new StringBuilder();\n            localStringBuilder.append(\"CREATE TABLE \");\n            localStringBuilder.append(paramString);\n            localStringBuilder.append(\" (\");\n            int i = paramArrayOfString.length;\n            for (int j = 0;; j++) {\n                if (j >= i) {\n                    localStringBuilder\n                            .setLength(localStringBuilder.length() - 1);\n                    localStringBuilder.append(\");\");\n                    return localStringBuilder.toString();\n                }\n                String[] arrayOfString = paramArrayOfString[j];\n                localStringBuilder.append(' ');\n                localStringBuilder.append(arrayOfString[0]);\n                localStringBuilder.append(' ');\n                localStringBuilder.append(arrayOfString[1]);\n                localStringBuilder.append(',');\n            }\n        }\n\n        /**\n         * These two arrays must match and have the same order. For every Schema\n         * there must be a corresponding table name.\n         */\n        static final private String[][][] sSchemas = {\n                DownloadColumns.SCHEMA, MetadataColumns.SCHEMA\n        };\n\n        static final private String[] sTables = {\n                DownloadColumns.TABLE_NAME, MetadataColumns.TABLE_NAME\n        };\n\n        /**\n         * Goes through all of the tables in sTables and drops each table if it\n         * exists. Altered to no longer make use of reflection.\n         */\n        private void dropTables(SQLiteDatabase paramSQLiteDatabase) {\n            for (String table : sTables) {\n                try {\n                    paramSQLiteDatabase.execSQL(\"DROP TABLE IF EXISTS \" + table);\n                } catch (Exception localException) {\n                    localException.printStackTrace();\n                }\n            }\n        }\n\n        /**\n         * Goes through all of the tables in sTables and creates a database with\n         * the corresponding schema described in sSchemas. Altered to no longer\n         * make use of reflection.\n         */\n        public void onCreate(SQLiteDatabase paramSQLiteDatabase) {\n            int numSchemas = sSchemas.length;\n            for (int i = 0; i < numSchemas; i++) {\n                try {\n                    String[][] schema = (String[][]) sSchemas[i];\n                    paramSQLiteDatabase.execSQL(createTableQueryFromArray(\n                            sTables[i], schema));\n                } catch (Exception localException) {\n                    while (true)\n                        localException.printStackTrace();\n                }\n            }\n        }\n\n        public void onUpgrade(SQLiteDatabase paramSQLiteDatabase,\n                int paramInt1, int paramInt2) {\n            Log.w(DownloadsContentDBHelper.class.getName(),\n                    \"Upgrading database from version \" + paramInt1 + \" to \"\n                            + paramInt2 + \", which will destroy all old data\");\n            dropTables(paramSQLiteDatabase);\n            onCreate(paramSQLiteDatabase);\n        }\n    }\n\n    public static class MetadataColumns implements BaseColumns {\n        public static final String APKVERSION = \"APKVERSION\";\n        public static final String DOWNLOAD_STATUS = \"DOWNLOADSTATUS\";\n        public static final String FLAGS = \"DOWNLOADFLAGS\";\n\n        public static final String[][] SCHEMA = {\n                {\n                        BaseColumns._ID, \"INTEGER PRIMARY KEY\"\n                },\n                {\n                        APKVERSION, \"INTEGER\"\n                }, {\n                        DOWNLOAD_STATUS, \"INTEGER\"\n                },\n                {\n                        FLAGS, \"INTEGER\"\n                }\n        };\n        public static final String TABLE_NAME = \"MetadataColumns\";\n        public static final String _ID = \"MetadataColumns._id\";\n    }\n\n    public static class DownloadColumns implements BaseColumns {\n        public static final String INDEX = \"FILEIDX\";\n        public static final String URI = \"URI\";\n        public static final String FILENAME = \"FN\";\n        public static final String ETAG = \"ETAG\";\n\n        public static final String TOTALBYTES = \"TOTALBYTES\";\n        public static final String CURRENTBYTES = \"CURRENTBYTES\";\n        public static final String LASTMOD = \"LASTMOD\";\n\n        public static final String STATUS = \"STATUS\";\n        public static final String CONTROL = \"CONTROL\";\n        public static final String NUM_FAILED = \"FAILCOUNT\";\n        public static final String RETRY_AFTER = \"RETRYAFTER\";\n        public static final String REDIRECT_COUNT = \"REDIRECTCOUNT\";\n\n        public static final String[][] SCHEMA = {\n                {\n                        BaseColumns._ID, \"INTEGER PRIMARY KEY\"\n                },\n                {\n                        INDEX, \"INTEGER UNIQUE\"\n                }, {\n                        URI, \"TEXT\"\n                },\n                {\n                        FILENAME, \"TEXT UNIQUE\"\n                }, {\n                        ETAG, \"TEXT\"\n                },\n                {\n                        TOTALBYTES, \"INTEGER\"\n                }, {\n                        CURRENTBYTES, \"INTEGER\"\n                },\n                {\n                        LASTMOD, \"INTEGER\"\n                }, {\n                        STATUS, \"INTEGER\"\n                },\n                {\n                        CONTROL, \"INTEGER\"\n                }, {\n                        NUM_FAILED, \"INTEGER\"\n                },\n                {\n                        RETRY_AFTER, \"INTEGER\"\n                }, {\n                        REDIRECT_COUNT, \"INTEGER\"\n                }\n        };\n        public static final String TABLE_NAME = \"DownloadColumns\";\n        public static final String _ID = \"DownloadColumns._id\";\n    }\n\n    private static final String[] DC_PROJECTION = {\n            DownloadColumns.FILENAME,\n            DownloadColumns.URI, DownloadColumns.ETAG,\n            DownloadColumns.TOTALBYTES, DownloadColumns.CURRENTBYTES,\n            DownloadColumns.LASTMOD, DownloadColumns.STATUS,\n            DownloadColumns.CONTROL, DownloadColumns.NUM_FAILED,\n            DownloadColumns.RETRY_AFTER, DownloadColumns.REDIRECT_COUNT,\n            DownloadColumns.INDEX\n    };\n\n    private static final int FILENAME_IDX = 0;\n    private static final int URI_IDX = 1;\n    private static final int ETAG_IDX = 2;\n    private static final int TOTALBYTES_IDX = 3;\n    private static final int CURRENTBYTES_IDX = 4;\n    private static final int LASTMOD_IDX = 5;\n    private static final int STATUS_IDX = 6;\n    private static final int CONTROL_IDX = 7;\n    private static final int NUM_FAILED_IDX = 8;\n    private static final int RETRY_AFTER_IDX = 9;\n    private static final int REDIRECT_COUNT_IDX = 10;\n    private static final int INDEX_IDX = 11;\n\n    /**\n     * This function will add a new file to the database if it does not exist.\n     * \n     * @param di DownloadInfo that we wish to store\n     * @return the row id of the record to be updated/inserted, or -1\n     */\n    public boolean updateDownload(DownloadInfo di) {\n        ContentValues cv = new ContentValues();\n        cv.put(DownloadColumns.INDEX, di.mIndex);\n        cv.put(DownloadColumns.FILENAME, di.mFileName);\n        cv.put(DownloadColumns.URI, di.mUri);\n        cv.put(DownloadColumns.ETAG, di.mETag);\n        cv.put(DownloadColumns.TOTALBYTES, di.mTotalBytes);\n        cv.put(DownloadColumns.CURRENTBYTES, di.mCurrentBytes);\n        cv.put(DownloadColumns.LASTMOD, di.mLastMod);\n        cv.put(DownloadColumns.STATUS, di.mStatus);\n        cv.put(DownloadColumns.CONTROL, di.mControl);\n        cv.put(DownloadColumns.NUM_FAILED, di.mNumFailed);\n        cv.put(DownloadColumns.RETRY_AFTER, di.mRetryAfter);\n        cv.put(DownloadColumns.REDIRECT_COUNT, di.mRedirectCount);\n        return updateDownload(di, cv);\n    }\n\n    public boolean updateDownload(DownloadInfo di, ContentValues cv) {\n        long id = di == null ? -1 : getIDForDownloadInfo(di);\n        try {\n            final SQLiteDatabase sqldb = mHelper.getWritableDatabase();\n            if (id != -1) {\n                if (1 != sqldb.update(DownloadColumns.TABLE_NAME,\n                        cv, DownloadColumns._ID + \" = \" + id, null)) {\n                    return false;\n                }\n            } else {\n                return -1 != sqldb.insert(DownloadColumns.TABLE_NAME,\n                        DownloadColumns.URI, cv);\n            }\n        } catch (android.database.sqlite.SQLiteException ex) {\n            ex.printStackTrace();\n        }\n        return false;\n    }\n\n    public int getLastCheckedVersionCode() {\n        return mVersionCode;\n    }\n\n    public boolean isDownloadRequired() {\n        final SQLiteDatabase sqldb = mHelper.getReadableDatabase();\n        Cursor cur = sqldb.rawQuery(\"SELECT Count(*) FROM \"\n                + DownloadColumns.TABLE_NAME + \" WHERE \"\n                + DownloadColumns.STATUS + \" <> 0\", null);\n        try {\n            if (null != cur && cur.moveToFirst()) {\n                return 0 == cur.getInt(0);\n            }\n        } finally {\n            if (null != cur)\n                cur.close();\n        }\n        return true;\n    }\n\n    public int getFlags() {\n        return mFlags;\n    }\n\n    public boolean updateFlags(int flags) {\n        if (mFlags != flags) {\n            ContentValues cv = new ContentValues();\n            cv.put(MetadataColumns.FLAGS, flags);\n            if (updateMetadata(cv)) {\n                mFlags = flags;\n                return true;\n            } else {\n                return false;\n            }\n        } else {\n            return true;\n        }\n    };\n\n    public boolean updateStatus(int status) {\n        if (mStatus != status) {\n            ContentValues cv = new ContentValues();\n            cv.put(MetadataColumns.DOWNLOAD_STATUS, status);\n            if (updateMetadata(cv)) {\n                mStatus = status;\n                return true;\n            } else {\n                return false;\n            }\n        } else {\n            return true;\n        }\n    };\n\n    public boolean updateMetadata(ContentValues cv) {\n        final SQLiteDatabase sqldb = mHelper.getWritableDatabase();\n        if (-1 == this.mMetadataRowID) {\n            long newID = sqldb.insert(MetadataColumns.TABLE_NAME,\n                    MetadataColumns.APKVERSION, cv);\n            if (-1 == newID)\n                return false;\n            mMetadataRowID = newID;\n        } else {\n            if (0 == sqldb.update(MetadataColumns.TABLE_NAME, cv,\n                    BaseColumns._ID + \" = \" + mMetadataRowID, null))\n                return false;\n        }\n        return true;\n    }\n\n    public boolean updateMetadata(int apkVersion, int downloadStatus) {\n        ContentValues cv = new ContentValues();\n        cv.put(MetadataColumns.APKVERSION, apkVersion);\n        cv.put(MetadataColumns.DOWNLOAD_STATUS, downloadStatus);\n        if (updateMetadata(cv)) {\n            mVersionCode = apkVersion;\n            mStatus = downloadStatus;\n            return true;\n        } else {\n            return false;\n        }\n    };\n\n    public boolean updateFromDb(DownloadInfo di) {\n        final SQLiteDatabase sqldb = mHelper.getReadableDatabase();\n        Cursor cur = null;\n        try {\n            cur = sqldb.query(DownloadColumns.TABLE_NAME, DC_PROJECTION,\n                    DownloadColumns.FILENAME + \"= ?\",\n                    new String[] {\n                        di.mFileName\n                    }, null, null, null);\n            if (null != cur && cur.moveToFirst()) {\n                setDownloadInfoFromCursor(di, cur);\n                return true;\n            }\n            return false;\n        } finally {\n            if (null != cur) {\n                cur.close();\n            }\n        }\n    }\n\n    public void setDownloadInfoFromCursor(DownloadInfo di, Cursor cur) {\n        di.mUri = cur.getString(URI_IDX);\n        di.mETag = cur.getString(ETAG_IDX);\n        di.mTotalBytes = cur.getLong(TOTALBYTES_IDX);\n        di.mCurrentBytes = cur.getLong(CURRENTBYTES_IDX);\n        di.mLastMod = cur.getLong(LASTMOD_IDX);\n        di.mStatus = cur.getInt(STATUS_IDX);\n        di.mControl = cur.getInt(CONTROL_IDX);\n        di.mNumFailed = cur.getInt(NUM_FAILED_IDX);\n        di.mRetryAfter = cur.getInt(RETRY_AFTER_IDX);\n        di.mRedirectCount = cur.getInt(REDIRECT_COUNT_IDX);\n    }\n\n    public DownloadInfo getDownloadInfoFromCursor(Cursor cur) {\n        DownloadInfo di = new DownloadInfo(cur.getInt(INDEX_IDX),\n                cur.getString(FILENAME_IDX), this.getClass().getPackage()\n                        .getName());\n        setDownloadInfoFromCursor(di, cur);\n        return di;\n    }\n\n    public DownloadInfo[] getDownloads() {\n        final SQLiteDatabase sqldb = mHelper.getReadableDatabase();\n        Cursor cur = null;\n        try {\n            cur = sqldb.query(DownloadColumns.TABLE_NAME, DC_PROJECTION, null,\n                    null, null, null, null);\n            if (null != cur && cur.moveToFirst()) {\n                DownloadInfo[] retInfos = new DownloadInfo[cur.getCount()];\n                int idx = 0;\n                do {\n                    DownloadInfo di = getDownloadInfoFromCursor(cur);\n                    retInfos[idx++] = di;\n                } while (cur.moveToNext());\n                return retInfos;\n            }\n            return null;\n        } finally {\n            if (null != cur) {\n                cur.close();\n            }\n        }\n    }\n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/expansion/downloader/impl/HttpDateTime.java",
    "content": "/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.expansion.downloader.impl;\n\nimport android.text.format.Time;\n\nimport java.util.Calendar;\nimport java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\n/**\n * Helper for parsing an HTTP date.\n */\npublic final class HttpDateTime {\n\n    /*\n     * Regular expression for parsing HTTP-date. Wdy, DD Mon YYYY HH:MM:SS GMT\n     * RFC 822, updated by RFC 1123 Weekday, DD-Mon-YY HH:MM:SS GMT RFC 850,\n     * obsoleted by RFC 1036 Wdy Mon DD HH:MM:SS YYYY ANSI C's asctime() format\n     * with following variations Wdy, DD-Mon-YYYY HH:MM:SS GMT Wdy, (SP)D Mon\n     * YYYY HH:MM:SS GMT Wdy,DD Mon YYYY HH:MM:SS GMT Wdy, DD-Mon-YY HH:MM:SS\n     * GMT Wdy, DD Mon YYYY HH:MM:SS -HHMM Wdy, DD Mon YYYY HH:MM:SS Wdy Mon\n     * (SP)D HH:MM:SS YYYY Wdy Mon DD HH:MM:SS YYYY GMT HH can be H if the first\n     * digit is zero. Mon can be the full name of the month.\n     */\n    private static final String HTTP_DATE_RFC_REGEXP =\n            \"([0-9]{1,2})[- ]([A-Za-z]{3,9})[- ]([0-9]{2,4})[ ]\"\n                    + \"([0-9]{1,2}:[0-9][0-9]:[0-9][0-9])\";\n\n    private static final String HTTP_DATE_ANSIC_REGEXP =\n            \"[ ]([A-Za-z]{3,9})[ ]+([0-9]{1,2})[ ]\"\n                    + \"([0-9]{1,2}:[0-9][0-9]:[0-9][0-9])[ ]([0-9]{2,4})\";\n\n    /**\n     * The compiled version of the HTTP-date regular expressions.\n     */\n    private static final Pattern HTTP_DATE_RFC_PATTERN =\n            Pattern.compile(HTTP_DATE_RFC_REGEXP);\n    private static final Pattern HTTP_DATE_ANSIC_PATTERN =\n            Pattern.compile(HTTP_DATE_ANSIC_REGEXP);\n\n    private static class TimeOfDay {\n        TimeOfDay(int h, int m, int s) {\n            this.hour = h;\n            this.minute = m;\n            this.second = s;\n        }\n\n        int hour;\n        int minute;\n        int second;\n    }\n\n    public static long parse(String timeString)\n            throws IllegalArgumentException {\n\n        int date = 1;\n        int month = Calendar.JANUARY;\n        int year = 1970;\n        TimeOfDay timeOfDay;\n\n        Matcher rfcMatcher = HTTP_DATE_RFC_PATTERN.matcher(timeString);\n        if (rfcMatcher.find()) {\n            date = getDate(rfcMatcher.group(1));\n            month = getMonth(rfcMatcher.group(2));\n            year = getYear(rfcMatcher.group(3));\n            timeOfDay = getTime(rfcMatcher.group(4));\n        } else {\n            Matcher ansicMatcher = HTTP_DATE_ANSIC_PATTERN.matcher(timeString);\n            if (ansicMatcher.find()) {\n                month = getMonth(ansicMatcher.group(1));\n                date = getDate(ansicMatcher.group(2));\n                timeOfDay = getTime(ansicMatcher.group(3));\n                year = getYear(ansicMatcher.group(4));\n            } else {\n                throw new IllegalArgumentException();\n            }\n        }\n\n        // FIXME: Y2038 BUG!\n        if (year >= 2038) {\n            year = 2038;\n            month = Calendar.JANUARY;\n            date = 1;\n        }\n\n        Time time = new Time(Time.TIMEZONE_UTC);\n        time.set(timeOfDay.second, timeOfDay.minute, timeOfDay.hour, date,\n                month, year);\n        return time.toMillis(false /* use isDst */);\n    }\n\n    private static int getDate(String dateString) {\n        if (dateString.length() == 2) {\n            return (dateString.charAt(0) - '0') * 10\n                    + (dateString.charAt(1) - '0');\n        } else {\n            return (dateString.charAt(0) - '0');\n        }\n    }\n\n    /*\n     * jan = 9 + 0 + 13 = 22 feb = 5 + 4 + 1 = 10 mar = 12 + 0 + 17 = 29 apr = 0\n     * + 15 + 17 = 32 may = 12 + 0 + 24 = 36 jun = 9 + 20 + 13 = 42 jul = 9 + 20\n     * + 11 = 40 aug = 0 + 20 + 6 = 26 sep = 18 + 4 + 15 = 37 oct = 14 + 2 + 19\n     * = 35 nov = 13 + 14 + 21 = 48 dec = 3 + 4 + 2 = 9\n     */\n    private static int getMonth(String monthString) {\n        int hash = Character.toLowerCase(monthString.charAt(0)) +\n                Character.toLowerCase(monthString.charAt(1)) +\n                Character.toLowerCase(monthString.charAt(2)) - 3 * 'a';\n        switch (hash) {\n            case 22:\n                return Calendar.JANUARY;\n            case 10:\n                return Calendar.FEBRUARY;\n            case 29:\n                return Calendar.MARCH;\n            case 32:\n                return Calendar.APRIL;\n            case 36:\n                return Calendar.MAY;\n            case 42:\n                return Calendar.JUNE;\n            case 40:\n                return Calendar.JULY;\n            case 26:\n                return Calendar.AUGUST;\n            case 37:\n                return Calendar.SEPTEMBER;\n            case 35:\n                return Calendar.OCTOBER;\n            case 48:\n                return Calendar.NOVEMBER;\n            case 9:\n                return Calendar.DECEMBER;\n            default:\n                throw new IllegalArgumentException();\n        }\n    }\n\n    private static int getYear(String yearString) {\n        if (yearString.length() == 2) {\n            int year = (yearString.charAt(0) - '0') * 10\n                    + (yearString.charAt(1) - '0');\n            if (year >= 70) {\n                return year + 1900;\n            } else {\n                return year + 2000;\n            }\n        } else if (yearString.length() == 3) {\n            // According to RFC 2822, three digit years should be added to 1900.\n            int year = (yearString.charAt(0) - '0') * 100\n                    + (yearString.charAt(1) - '0') * 10\n                    + (yearString.charAt(2) - '0');\n            return year + 1900;\n        } else if (yearString.length() == 4) {\n            return (yearString.charAt(0) - '0') * 1000\n                    + (yearString.charAt(1) - '0') * 100\n                    + (yearString.charAt(2) - '0') * 10\n                    + (yearString.charAt(3) - '0');\n        } else {\n            return 1970;\n        }\n    }\n\n    private static TimeOfDay getTime(String timeString) {\n        // HH might be H\n        int i = 0;\n        int hour = timeString.charAt(i++) - '0';\n        if (timeString.charAt(i) != ':')\n            hour = hour * 10 + (timeString.charAt(i++) - '0');\n        // Skip ':'\n        i++;\n\n        int minute = (timeString.charAt(i++) - '0') * 10\n                + (timeString.charAt(i++) - '0');\n        // Skip ':'\n        i++;\n\n        int second = (timeString.charAt(i++) - '0') * 10\n                + (timeString.charAt(i++) - '0');\n\n        return new TimeOfDay(hour, minute, second);\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/AESObfuscator.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\nimport com.google.android.vending.licensing.util.Base64;\nimport com.google.android.vending.licensing.util.Base64DecoderException;\n\nimport java.io.UnsupportedEncodingException;\nimport java.security.GeneralSecurityException;\nimport java.security.spec.KeySpec;\n\nimport javax.crypto.BadPaddingException;\nimport javax.crypto.Cipher;\nimport javax.crypto.IllegalBlockSizeException;\nimport javax.crypto.SecretKey;\nimport javax.crypto.SecretKeyFactory;\nimport javax.crypto.spec.IvParameterSpec;\nimport javax.crypto.spec.PBEKeySpec;\nimport javax.crypto.spec.SecretKeySpec;\n\n/**\n * An Obfuscator that uses AES to encrypt data.\n */\npublic class AESObfuscator implements Obfuscator {\n    private static final String UTF8 = \"UTF-8\";\n    private static final String KEYGEN_ALGORITHM = \"PBEWITHSHAAND256BITAES-CBC-BC\";\n    private static final String CIPHER_ALGORITHM = \"AES/CBC/PKCS5Padding\";\n    private static final byte[] IV =\n        { 16, 74, 71, -80, 32, 101, -47, 72, 117, -14, 0, -29, 70, 65, -12, 74 };\n    private static final String header = \"com.google.android.vending.licensing.AESObfuscator-1|\";\n\n    private Cipher mEncryptor;\n    private Cipher mDecryptor;\n\n    /**\n     * @param salt an array of random bytes to use for each (un)obfuscation\n     * @param applicationId application identifier, e.g. the package name\n     * @param deviceId device identifier. Use as many sources as possible to\n     *    create this unique identifier.\n     */\n    public AESObfuscator(byte[] salt, String applicationId, String deviceId) {\n        try {\n            SecretKeyFactory factory = SecretKeyFactory.getInstance(KEYGEN_ALGORITHM);\n            KeySpec keySpec =\n                new PBEKeySpec((applicationId + deviceId).toCharArray(), salt, 1024, 256);\n            SecretKey tmp = factory.generateSecret(keySpec);\n            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), \"AES\");\n            mEncryptor = Cipher.getInstance(CIPHER_ALGORITHM);\n            mEncryptor.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(IV));\n            mDecryptor = Cipher.getInstance(CIPHER_ALGORITHM);\n            mDecryptor.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(IV));\n        } catch (GeneralSecurityException e) {\n            // This can't happen on a compatible Android device.\n            throw new RuntimeException(\"Invalid environment\", e);\n        }\n    }\n\n    public String obfuscate(String original, String key) {\n        if (original == null) {\n            return null;\n        }\n        try {\n            // Header is appended as an integrity check\n            return Base64.encode(mEncryptor.doFinal((header + key + original).getBytes(UTF8)));\n        } catch (UnsupportedEncodingException e) {\n            throw new RuntimeException(\"Invalid environment\", e);\n        } catch (GeneralSecurityException e) {\n            throw new RuntimeException(\"Invalid environment\", e);\n        }\n    }\n\n    public String unobfuscate(String obfuscated, String key) throws ValidationException {\n        if (obfuscated == null) {\n            return null;\n        }\n        try {\n            String result = new String(mDecryptor.doFinal(Base64.decode(obfuscated)), UTF8);\n            // Check for presence of header. This serves as a final integrity check, for cases\n            // where the block size is correct during decryption.\n            int headerIndex = result.indexOf(header+key);\n            if (headerIndex != 0) {\n                throw new ValidationException(\"Header not found (invalid data or key)\" + \":\" +\n                        obfuscated);\n            }\n            return result.substring(header.length()+key.length(), result.length());\n        } catch (Base64DecoderException e) {\n            throw new ValidationException(e.getMessage() + \":\" + obfuscated);\n        } catch (IllegalBlockSizeException e) {\n            throw new ValidationException(e.getMessage() + \":\" + obfuscated);\n        } catch (BadPaddingException e) {\n            throw new ValidationException(e.getMessage() + \":\" + obfuscated);\n        } catch (UnsupportedEncodingException e) {\n            throw new RuntimeException(\"Invalid environment\", e);\n        }\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/APKExpansionPolicy.java",
    "content": "\npackage com.google.android.vending.licensing;\n\n/*\n * Copyright (C) 2012 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport android.content.Context;\nimport android.content.SharedPreferences;\nimport android.util.Log;\n\nimport com.google.android.vending.licensing.util.URIQueryDecoder;\n\nimport java.net.URI;\nimport java.net.URISyntaxException;\nimport java.util.HashMap;\nimport java.util.Map;\nimport java.util.Set;\nimport java.util.Vector;\n\n/**\n * Default policy. All policy decisions are based off of response data received\n * from the licensing service. Specifically, the licensing server sends the\n * following information: response validity period, error retry period, and\n * error retry count.\n * <p>\n * These values will vary based on the the way the application is configured in\n * the Google Play publishing console, such as whether the application is\n * marked as free or is within its refund period, as well as how often an\n * application is checking with the licensing service.\n * <p>\n * Developers who need more fine grained control over their application's\n * licensing policy should implement a custom Policy.\n */\npublic class APKExpansionPolicy implements Policy {\n\n    private static final String TAG = \"APKExpansionPolicy\";\n    private static final String PREFS_FILE = \"com.google.android.vending.licensing.APKExpansionPolicy\";\n    private static final String PREF_LAST_RESPONSE = \"lastResponse\";\n    private static final String PREF_VALIDITY_TIMESTAMP = \"validityTimestamp\";\n    private static final String PREF_RETRY_UNTIL = \"retryUntil\";\n    private static final String PREF_MAX_RETRIES = \"maxRetries\";\n    private static final String PREF_RETRY_COUNT = \"retryCount\";\n    private static final String DEFAULT_VALIDITY_TIMESTAMP = \"0\";\n    private static final String DEFAULT_RETRY_UNTIL = \"0\";\n    private static final String DEFAULT_MAX_RETRIES = \"0\";\n    private static final String DEFAULT_RETRY_COUNT = \"0\";\n\n    private static final long MILLIS_PER_MINUTE = 60 * 1000;\n\n    private long mValidityTimestamp;\n    private long mRetryUntil;\n    private long mMaxRetries;\n    private long mRetryCount;\n    private long mLastResponseTime = 0;\n    private int mLastResponse;\n    private PreferenceObfuscator mPreferences;\n    private Vector<String> mExpansionURLs = new Vector<String>();\n    private Vector<String> mExpansionFileNames = new Vector<String>();\n    private Vector<Long> mExpansionFileSizes = new Vector<Long>();\n\n    /**\n     * The design of the protocol supports n files. Currently the market can\n     * only deliver two files. To accommodate this, we have these two constants,\n     * but the order is the only relevant thing here.\n     */\n    public static final int MAIN_FILE_URL_INDEX = 0;\n    public static final int PATCH_FILE_URL_INDEX = 1;\n\n    /**\n     * @param context The context for the current application\n     * @param obfuscator An obfuscator to be used with preferences.\n     */\n    public APKExpansionPolicy(Context context, Obfuscator obfuscator) {\n        // Import old values\n        SharedPreferences sp = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);\n        mPreferences = new PreferenceObfuscator(sp, obfuscator);\n        mLastResponse = Integer.parseInt(\n                mPreferences.getString(PREF_LAST_RESPONSE, Integer.toString(Policy.RETRY)));\n        mValidityTimestamp = Long.parseLong(mPreferences.getString(PREF_VALIDITY_TIMESTAMP,\n                DEFAULT_VALIDITY_TIMESTAMP));\n        mRetryUntil = Long.parseLong(mPreferences.getString(PREF_RETRY_UNTIL, DEFAULT_RETRY_UNTIL));\n        mMaxRetries = Long.parseLong(mPreferences.getString(PREF_MAX_RETRIES, DEFAULT_MAX_RETRIES));\n        mRetryCount = Long.parseLong(mPreferences.getString(PREF_RETRY_COUNT, DEFAULT_RETRY_COUNT));\n    }\n\n    /**\n     * We call this to guarantee that we fetch a fresh policy from the server.\n     * This is to be used if the URL is invalid.\n     */\n    public void resetPolicy() {\n        mPreferences.putString(PREF_LAST_RESPONSE, Integer.toString(Policy.RETRY));\n        setRetryUntil(DEFAULT_RETRY_UNTIL);\n        setMaxRetries(DEFAULT_MAX_RETRIES);\n        setRetryCount(Long.parseLong(DEFAULT_RETRY_COUNT));\n        setValidityTimestamp(DEFAULT_VALIDITY_TIMESTAMP);\n        mPreferences.commit();\n    }\n\n    /**\n     * Process a new response from the license server.\n     * <p>\n     * This data will be used for computing future policy decisions. The\n     * following parameters are processed:\n     * <ul>\n     * <li>VT: the timestamp that the client should consider the response valid\n     * until\n     * <li>GT: the timestamp that the client should ignore retry errors until\n     * <li>GR: the number of retry errors that the client should ignore\n     * </ul>\n     *\n     * @param response the result from validating the server response\n     * @param rawData the raw server response data\n     */\n    public void processServerResponse(int response,\n            com.google.android.vending.licensing.ResponseData rawData) {\n\n        // Update retry counter\n        if (response != Policy.RETRY) {\n            setRetryCount(0);\n        } else {\n            setRetryCount(mRetryCount + 1);\n        }\n\n        if (response == Policy.LICENSED) {\n            // Update server policy data\n            Map<String, String> extras = decodeExtras(rawData.extra);\n            mLastResponse = response;\n            setValidityTimestamp(Long.toString(System.currentTimeMillis() + MILLIS_PER_MINUTE));\n            Set<String> keys = extras.keySet();\n            for (String key : keys) {\n                if (key.equals(\"VT\")) {\n                    setValidityTimestamp(extras.get(key));\n                } else if (key.equals(\"GT\")) {\n                    setRetryUntil(extras.get(key));\n                } else if (key.equals(\"GR\")) {\n                    setMaxRetries(extras.get(key));\n                } else if (key.startsWith(\"FILE_URL\")) {\n                    int index = Integer.parseInt(key.substring(\"FILE_URL\".length())) - 1;\n                    setExpansionURL(index, extras.get(key));\n                } else if (key.startsWith(\"FILE_NAME\")) {\n                    int index = Integer.parseInt(key.substring(\"FILE_NAME\".length())) - 1;\n                    setExpansionFileName(index, extras.get(key));\n                } else if (key.startsWith(\"FILE_SIZE\")) {\n                    int index = Integer.parseInt(key.substring(\"FILE_SIZE\".length())) - 1;\n                    setExpansionFileSize(index, Long.parseLong(extras.get(key)));\n                }\n            }\n        } else if (response == Policy.NOT_LICENSED) {\n            // Clear out stale policy data\n            setValidityTimestamp(DEFAULT_VALIDITY_TIMESTAMP);\n            setRetryUntil(DEFAULT_RETRY_UNTIL);\n            setMaxRetries(DEFAULT_MAX_RETRIES);\n        }\n\n        setLastResponse(response);\n        mPreferences.commit();\n    }\n\n    /**\n     * Set the last license response received from the server and add to\n     * preferences. You must manually call PreferenceObfuscator.commit() to\n     * commit these changes to disk.\n     *\n     * @param l the response\n     */\n    private void setLastResponse(int l) {\n        mLastResponseTime = System.currentTimeMillis();\n        mLastResponse = l;\n        mPreferences.putString(PREF_LAST_RESPONSE, Integer.toString(l));\n    }\n\n    /**\n     * Set the current retry count and add to preferences. You must manually\n     * call PreferenceObfuscator.commit() to commit these changes to disk.\n     *\n     * @param c the new retry count\n     */\n    private void setRetryCount(long c) {\n        mRetryCount = c;\n        mPreferences.putString(PREF_RETRY_COUNT, Long.toString(c));\n    }\n\n    public long getRetryCount() {\n        return mRetryCount;\n    }\n\n    /**\n     * Set the last validity timestamp (VT) received from the server and add to\n     * preferences. You must manually call PreferenceObfuscator.commit() to\n     * commit these changes to disk.\n     *\n     * @param validityTimestamp the VT string received\n     */\n    private void setValidityTimestamp(String validityTimestamp) {\n        Long lValidityTimestamp;\n        try {\n            lValidityTimestamp = Long.parseLong(validityTimestamp);\n        } catch (NumberFormatException e) {\n            // No response or not parseable, expire in one minute.\n            Log.w(TAG, \"License validity timestamp (VT) missing, caching for a minute\");\n            lValidityTimestamp = System.currentTimeMillis() + MILLIS_PER_MINUTE;\n            validityTimestamp = Long.toString(lValidityTimestamp);\n        }\n\n        mValidityTimestamp = lValidityTimestamp;\n        mPreferences.putString(PREF_VALIDITY_TIMESTAMP, validityTimestamp);\n    }\n\n    public long getValidityTimestamp() {\n        return mValidityTimestamp;\n    }\n\n    /**\n     * Set the retry until timestamp (GT) received from the server and add to\n     * preferences. You must manually call PreferenceObfuscator.commit() to\n     * commit these changes to disk.\n     *\n     * @param retryUntil the GT string received\n     */\n    private void setRetryUntil(String retryUntil) {\n        Long lRetryUntil;\n        try {\n            lRetryUntil = Long.parseLong(retryUntil);\n        } catch (NumberFormatException e) {\n            // No response or not parseable, expire immediately\n            Log.w(TAG, \"License retry timestamp (GT) missing, grace period disabled\");\n            retryUntil = \"0\";\n            lRetryUntil = 0l;\n        }\n\n        mRetryUntil = lRetryUntil;\n        mPreferences.putString(PREF_RETRY_UNTIL, retryUntil);\n    }\n\n    public long getRetryUntil() {\n        return mRetryUntil;\n    }\n\n    /**\n     * Set the max retries value (GR) as received from the server and add to\n     * preferences. You must manually call PreferenceObfuscator.commit() to\n     * commit these changes to disk.\n     *\n     * @param maxRetries the GR string received\n     */\n    private void setMaxRetries(String maxRetries) {\n        Long lMaxRetries;\n        try {\n            lMaxRetries = Long.parseLong(maxRetries);\n        } catch (NumberFormatException e) {\n            // No response or not parseable, expire immediately\n            Log.w(TAG, \"Licence retry count (GR) missing, grace period disabled\");\n            maxRetries = \"0\";\n            lMaxRetries = 0l;\n        }\n\n        mMaxRetries = lMaxRetries;\n        mPreferences.putString(PREF_MAX_RETRIES, maxRetries);\n    }\n\n    public long getMaxRetries() {\n        return mMaxRetries;\n    }\n\n    /**\n     * Gets the count of expansion URLs. Since expansionURLs are not committed\n     * to preferences, this will return zero if there has been no LVL fetch\n     * in the current session.\n     *\n     * @return the number of expansion URLs. (0,1,2)\n     */\n    public int getExpansionURLCount() {\n        return mExpansionURLs.size();\n    }\n\n    /**\n     * Gets the expansion URL. Since these URLs are not committed to\n     * preferences, this will always return null if there has not been an LVL\n     * fetch in the current session.\n     *\n     * @param index the index of the URL to fetch. This value will be either\n     *            MAIN_FILE_URL_INDEX or PATCH_FILE_URL_INDEX\n     */\n    public String getExpansionURL(int index) {\n        if (index < mExpansionURLs.size()) {\n            return mExpansionURLs.elementAt(index);\n        }\n        return null;\n    }\n\n    /**\n     * Sets the expansion URL. Expansion URL's are not committed to preferences,\n     * but are instead intended to be stored when the license response is\n     * processed by the front-end.\n     *\n     * @param index the index of the expansion URL. This value will be either\n     *            MAIN_FILE_URL_INDEX or PATCH_FILE_URL_INDEX\n     * @param URL the URL to set\n     */\n    public void setExpansionURL(int index, String URL) {\n        if (index >= mExpansionURLs.size()) {\n            mExpansionURLs.setSize(index + 1);\n        }\n        mExpansionURLs.set(index, URL);\n    }\n\n    public String getExpansionFileName(int index) {\n        if (index < mExpansionFileNames.size()) {\n            return mExpansionFileNames.elementAt(index);\n        }\n        return null;\n    }\n\n    public void setExpansionFileName(int index, String name) {\n        if (index >= mExpansionFileNames.size()) {\n            mExpansionFileNames.setSize(index + 1);\n        }\n        mExpansionFileNames.set(index, name);\n    }\n\n    public long getExpansionFileSize(int index) {\n        if (index < mExpansionFileSizes.size()) {\n            return mExpansionFileSizes.elementAt(index);\n        }\n        return -1;\n    }\n\n    public void setExpansionFileSize(int index, long size) {\n        if (index >= mExpansionFileSizes.size()) {\n            mExpansionFileSizes.setSize(index + 1);\n        }\n        mExpansionFileSizes.set(index, size);\n    }\n\n    /**\n     * {@inheritDoc} This implementation allows access if either:<br>\n     * <ol>\n     * <li>a LICENSED response was received within the validity period\n     * <li>a RETRY response was received in the last minute, and we are under\n     * the RETRY count or in the RETRY period.\n     * </ol>\n     */\n    public boolean allowAccess() {\n        long ts = System.currentTimeMillis();\n        if (mLastResponse == Policy.LICENSED) {\n            // Check if the LICENSED response occurred within the validity\n            // timeout.\n            if (ts <= mValidityTimestamp) {\n                // Cached LICENSED response is still valid.\n                return true;\n            }\n        } else if (mLastResponse == Policy.RETRY &&\n                ts < mLastResponseTime + MILLIS_PER_MINUTE) {\n            // Only allow access if we are within the retry period or we haven't\n            // used up our\n            // max retries.\n            return (ts <= mRetryUntil || mRetryCount <= mMaxRetries);\n        }\n        return false;\n    }\n\n    private Map<String, String> decodeExtras(String extras) {\n        Map<String, String> results = new HashMap<String, String>();\n        try {\n            URI rawExtras = new URI(\"?\" + extras);\n            URIQueryDecoder.DecodeQuery(rawExtras, results);\n        } catch (URISyntaxException e) {\n            Log.w(TAG, \"Invalid syntax error while decoding extras data from server.\");\n        }\n        return results;\n    }\n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/DeviceLimiter.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\n/**\n * Allows the developer to limit the number of devices using a single license.\n * <p>\n * The LICENSED response from the server contains a user identifier unique to\n * the &lt;application, user&gt; pair. The developer can send this identifier\n * to their own server along with some device identifier (a random number\n * generated and stored once per application installation,\n * {@link android.telephony.TelephonyManager#getDeviceId getDeviceId},\n * {@link android.provider.Settings.Secure#ANDROID_ID ANDROID_ID}, etc).\n * The more sources used to identify the device, the harder it will be for an\n * attacker to spoof.\n * <p>\n * The server can look at the &lt;application, user, device id&gt; tuple and\n * restrict a user's application license to run on at most 10 different devices\n * in a week (for example). We recommend not being too restrictive because a\n * user might legitimately have multiple devices or be in the process of\n * changing phones. This will catch egregious violations of multiple people\n * sharing one license.\n */\npublic interface DeviceLimiter {\n\n    /**\n     * Checks if this device is allowed to use the given user's license.\n     *\n     * @param userId the user whose license the server responded with\n     * @return LICENSED if the device is allowed, NOT_LICENSED if not, RETRY if an error occurs\n     */\n    int isDeviceAllowed(String userId);\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/LicenseChecker.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\nimport android.content.ComponentName;\nimport android.content.Context;\nimport android.content.Intent;\nimport android.content.ServiceConnection;\nimport android.content.pm.PackageManager.NameNotFoundException;\nimport android.os.Handler;\nimport android.os.HandlerThread;\nimport android.os.IBinder;\nimport android.os.RemoteException;\nimport android.provider.Settings.Secure;\nimport android.util.Log;\n\nimport com.android.vending.licensing.ILicenseResultListener;\nimport com.android.vending.licensing.ILicensingService;\nimport com.google.android.vending.licensing.util.Base64;\nimport com.google.android.vending.licensing.util.Base64DecoderException;\n\nimport java.security.KeyFactory;\nimport java.security.NoSuchAlgorithmException;\nimport java.security.PublicKey;\nimport java.security.SecureRandom;\nimport java.security.spec.InvalidKeySpecException;\nimport java.security.spec.X509EncodedKeySpec;\nimport java.util.Date;\nimport java.util.HashSet;\nimport java.util.LinkedList;\nimport java.util.Queue;\nimport java.util.Set;\n\n/**\n * Client library for Google Play license verifications.\n * <p>\n * The LicenseChecker is configured via a {@link Policy} which contains the logic to determine\n * whether a user should have access to the application. For example, the Policy can define a\n * threshold for allowable number of server or client failures before the library reports the user\n * as not having access.\n * <p>\n * Must also provide the Base64-encoded RSA public key associated with your developer account. The\n * public key is obtainable from the publisher site.\n */\npublic class LicenseChecker implements ServiceConnection {\n    private static final String TAG = \"LicenseChecker\";\n\n    private static final String KEY_FACTORY_ALGORITHM = \"RSA\";\n\n    // Timeout value (in milliseconds) for calls to service.\n    private static final int TIMEOUT_MS = 10 * 1000;\n\n    private static final SecureRandom RANDOM = new SecureRandom();\n    private static final boolean DEBUG_LICENSE_ERROR = false;\n\n    private ILicensingService mService;\n\n    private PublicKey mPublicKey;\n    private final Context mContext;\n    private final Policy mPolicy;\n    /**\n     * A handler for running tasks on a background thread. We don't want license processing to block\n     * the UI thread.\n     */\n    private Handler mHandler;\n    private final String mPackageName;\n    private final String mVersionCode;\n    private final Set<LicenseValidator> mChecksInProgress = new HashSet<LicenseValidator>();\n    private final Queue<LicenseValidator> mPendingChecks = new LinkedList<LicenseValidator>();\n\n    /**\n     * @param context a Context\n     * @param policy implementation of Policy\n     * @param encodedPublicKey Base64-encoded RSA public key\n     * @throws IllegalArgumentException if encodedPublicKey is invalid\n     */\n    public LicenseChecker(Context context, Policy policy, String encodedPublicKey) {\n        mContext = context;\n        mPolicy = policy;\n        mPublicKey = generatePublicKey(encodedPublicKey);\n        mPackageName = mContext.getPackageName();\n        mVersionCode = getVersionCode(context, mPackageName);\n        HandlerThread handlerThread = new HandlerThread(\"background thread\");\n        handlerThread.start();\n        mHandler = new Handler(handlerThread.getLooper());\n    }\n\n    /**\n     * Generates a PublicKey instance from a string containing the Base64-encoded public key.\n     *\n     * @param encodedPublicKey Base64-encoded public key\n     * @throws IllegalArgumentException if encodedPublicKey is invalid\n     */\n    private static PublicKey generatePublicKey(String encodedPublicKey) {\n        try {\n            byte[] decodedKey = Base64.decode(encodedPublicKey);\n            KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);\n\n            return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));\n        } catch (NoSuchAlgorithmException e) {\n            // This won't happen in an Android-compatible environment.\n            throw new RuntimeException(e);\n        } catch (Base64DecoderException e) {\n            Log.e(TAG, \"Could not decode from Base64.\");\n            throw new IllegalArgumentException(e);\n        } catch (InvalidKeySpecException e) {\n            Log.e(TAG, \"Invalid key specification.\");\n            throw new IllegalArgumentException(e);\n        }\n    }\n\n    /**\n     * Checks if the user should have access to the app. Binds the service if necessary.\n     * <p>\n     * NOTE: This call uses a trivially obfuscated string (base64-encoded). For best security, we\n     * recommend obfuscating the string that is passed into bindService using another method of your\n     * own devising.\n     * <p>\n     * source string: \"com.android.vending.licensing.ILicensingService\"\n     * <p>\n     * \n     * @param callback\n     */\n    public synchronized void checkAccess(LicenseCheckerCallback callback) {\n        // If we have a valid recent LICENSED response, we can skip asking\n        // Market.\n        if (mPolicy.allowAccess()) {\n            Log.i(TAG, \"Using cached license response\");\n            callback.allow(Policy.LICENSED);\n        } else {\n            LicenseValidator validator = new LicenseValidator(mPolicy, new NullDeviceLimiter(),\n                    callback, generateNonce(), mPackageName, mVersionCode);\n\n            if (mService == null) {\n                Log.i(TAG, \"Binding to licensing service.\");\n                try {\n                    boolean bindResult = mContext\n                            .bindService(\n                                    new Intent(\n                                            new String(\n                                                    // Base64 encoded -\n                                                    // com.android.vending.licensing.ILicensingService\n                                                    // Consider encoding this in another way in your\n                                                    // code to improve security\n                                                    Base64.decode(\n                                                            \"Y29tLmFuZHJvaWQudmVuZGluZy5saWNlbnNpbmcuSUxpY2Vuc2luZ1NlcnZpY2U=\")))\n                                                                    // As of Android 5.0, implicit\n                                                                    // Service Intents are no longer\n                                                                    // allowed because it's not\n                                                                    // possible for the user to\n                                                                    // participate in disambiguating\n                                                                    // them. This does mean we break\n                                                                    // compatibility with Android\n                                                                    // Cupcake devices with this\n                                                                    // release, since setPackage was\n                                                                    // added in Donut.\n                                                                    .setPackage(\n                                                                            new String(\n                                                                                    // Base64\n                                                                                    // encoded -\n                                                                                    // com.android.vending\n                                                                                    Base64.decode(\n                                                                                            \"Y29tLmFuZHJvaWQudmVuZGluZw==\"))),\n                                    this, // ServiceConnection.\n                                    Context.BIND_AUTO_CREATE);\n                    if (bindResult) {\n                        mPendingChecks.offer(validator);\n                    } else {\n                        Log.e(TAG, \"Could not bind to service.\");\n                        handleServiceConnectionError(validator);\n                    }\n                } catch (SecurityException e) {\n                    callback.applicationError(LicenseCheckerCallback.ERROR_MISSING_PERMISSION);\n                } catch (Base64DecoderException e) {\n                    e.printStackTrace();\n                }\n            } else {\n                mPendingChecks.offer(validator);\n                runChecks();\n            }\n        }\n    }\n\n    private void runChecks() {\n        LicenseValidator validator;\n        while ((validator = mPendingChecks.poll()) != null) {\n            try {\n                Log.i(TAG, \"Calling checkLicense on service for \" + validator.getPackageName());\n                mService.checkLicense(\n                        validator.getNonce(), validator.getPackageName(),\n                        new ResultListener(validator));\n                mChecksInProgress.add(validator);\n            } catch (RemoteException e) {\n                Log.w(TAG, \"RemoteException in checkLicense call.\", e);\n                handleServiceConnectionError(validator);\n            }\n        }\n    }\n\n    private synchronized void finishCheck(LicenseValidator validator) {\n        mChecksInProgress.remove(validator);\n        if (mChecksInProgress.isEmpty()) {\n            cleanupService();\n        }\n    }\n\n    private class ResultListener extends ILicenseResultListener.Stub {\n        private final LicenseValidator mValidator;\n        private Runnable mOnTimeout;\n\n        public ResultListener(LicenseValidator validator) {\n            mValidator = validator;\n            mOnTimeout = new Runnable() {\n                public void run() {\n                    Log.i(TAG, \"Check timed out.\");\n                    handleServiceConnectionError(mValidator);\n                    finishCheck(mValidator);\n                }\n            };\n            startTimeout();\n        }\n\n        private static final int ERROR_CONTACTING_SERVER = 0x101;\n        private static final int ERROR_INVALID_PACKAGE_NAME = 0x102;\n        private static final int ERROR_NON_MATCHING_UID = 0x103;\n\n        // Runs in IPC thread pool. Post it to the Handler, so we can guarantee\n        // either this or the timeout runs.\n        public void verifyLicense(final int responseCode, final String signedData,\n                final String signature) {\n            mHandler.post(new Runnable() {\n                public void run() {\n                    Log.i(TAG, \"Received response.\");\n                    // Make sure it hasn't already timed out.\n                    if (mChecksInProgress.contains(mValidator)) {\n                        clearTimeout();\n                        mValidator.verify(mPublicKey, responseCode, signedData, signature);\n                        finishCheck(mValidator);\n                    }\n                    if (DEBUG_LICENSE_ERROR) {\n                        boolean logResponse;\n                        String stringError = null;\n                        switch (responseCode) {\n                            case ERROR_CONTACTING_SERVER:\n                                logResponse = true;\n                                stringError = \"ERROR_CONTACTING_SERVER\";\n                                break;\n                            case ERROR_INVALID_PACKAGE_NAME:\n                                logResponse = true;\n                                stringError = \"ERROR_INVALID_PACKAGE_NAME\";\n                                break;\n                            case ERROR_NON_MATCHING_UID:\n                                logResponse = true;\n                                stringError = \"ERROR_NON_MATCHING_UID\";\n                                break;\n                            default:\n                                logResponse = false;\n                        }\n\n                        if (logResponse) {\n                            String android_id = Secure.getString(mContext.getContentResolver(),\n                                    Secure.ANDROID_ID);\n                            Date date = new Date();\n                            Log.d(TAG, \"Server Failure: \" + stringError);\n                            Log.d(TAG, \"Android ID: \" + android_id);\n                            Log.d(TAG, \"Time: \" + date.toGMTString());\n                        }\n                    }\n\n                }\n            });\n        }\n\n        private void startTimeout() {\n            Log.i(TAG, \"Start monitoring timeout.\");\n            mHandler.postDelayed(mOnTimeout, TIMEOUT_MS);\n        }\n\n        private void clearTimeout() {\n            Log.i(TAG, \"Clearing timeout.\");\n            mHandler.removeCallbacks(mOnTimeout);\n        }\n    }\n\n    public synchronized void onServiceConnected(ComponentName name, IBinder service) {\n        mService = ILicensingService.Stub.asInterface(service);\n        runChecks();\n    }\n\n    public synchronized void onServiceDisconnected(ComponentName name) {\n        // Called when the connection with the service has been\n        // unexpectedly disconnected. That is, Market crashed.\n        // If there are any checks in progress, the timeouts will handle them.\n        Log.w(TAG, \"Service unexpectedly disconnected.\");\n        mService = null;\n    }\n\n    /**\n     * Generates policy response for service connection errors, as a result of disconnections or\n     * timeouts.\n     */\n    private synchronized void handleServiceConnectionError(LicenseValidator validator) {\n        mPolicy.processServerResponse(Policy.RETRY, null);\n\n        if (mPolicy.allowAccess()) {\n            validator.getCallback().allow(Policy.RETRY);\n        } else {\n            validator.getCallback().dontAllow(Policy.RETRY);\n        }\n    }\n\n    /** Unbinds service if necessary and removes reference to it. */\n    private void cleanupService() {\n        if (mService != null) {\n            try {\n                mContext.unbindService(this);\n            } catch (IllegalArgumentException e) {\n                // Somehow we've already been unbound. This is a non-fatal\n                // error.\n                Log.e(TAG, \"Unable to unbind from licensing service (already unbound)\");\n            }\n            mService = null;\n        }\n    }\n\n    /**\n     * Inform the library that the context is about to be destroyed, so that any open connections\n     * can be cleaned up.\n     * <p>\n     * Failure to call this method can result in a crash under certain circumstances, such as during\n     * screen rotation if an Activity requests the license check or when the user exits the\n     * application.\n     */\n    public synchronized void onDestroy() {\n        cleanupService();\n        mHandler.getLooper().quit();\n    }\n\n    /** Generates a nonce (number used once). */\n    private int generateNonce() {\n        return RANDOM.nextInt();\n    }\n\n    /**\n     * Get version code for the application package name.\n     *\n     * @param context\n     * @param packageName application package name\n     * @return the version code or empty string if package not found\n     */\n    private static String getVersionCode(Context context, String packageName) {\n        try {\n            return String.valueOf(\n                    context.getPackageManager().getPackageInfo(packageName, 0).versionCode);\n        } catch (NameNotFoundException e) {\n            Log.e(TAG, \"Package not found. could not get version code.\");\n            return \"\";\n        }\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/LicenseCheckerCallback.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\n/**\n * Callback for the license checker library.\n * <p>\n * Upon checking with the Market server and conferring with the {@link Policy},\n * the library calls the appropriate callback method to communicate the result.\n * <p>\n * <b>The callback does not occur in the original checking thread.</b> Your\n * application should post to the appropriate handling thread or lock\n * accordingly.\n * <p>\n * The reason that is passed back with allow/dontAllow is the base status handed\n * to the policy for allowed/disallowing the license. Policy.RETRY will call\n * allow or dontAllow depending on other statistics associated with the policy,\n * while in most cases Policy.NOT_LICENSED will call dontAllow and\n * Policy.LICENSED will Allow.\n */\npublic interface LicenseCheckerCallback {\n\n    /**\n     * Allow use. App should proceed as normal.\n     * \n     * @param reason Policy.LICENSED or Policy.RETRY typically. (although in\n     *            theory the policy can return Policy.NOT_LICENSED here as well)\n     */\n    public void allow(int reason);\n\n    /**\n     * Don't allow use. App should inform user and take appropriate action.\n     * \n     * @param reason Policy.NOT_LICENSED or Policy.RETRY. (although in theory\n     *            the policy can return Policy.LICENSED here as well ---\n     *            perhaps the call to the LVL took too long, for example)\n     */\n    public void dontAllow(int reason);\n\n    /** Application error codes. */\n    public static final int ERROR_INVALID_PACKAGE_NAME = 1;\n    public static final int ERROR_NON_MATCHING_UID = 2;\n    public static final int ERROR_NOT_MARKET_MANAGED = 3;\n    public static final int ERROR_CHECK_IN_PROGRESS = 4;\n    public static final int ERROR_INVALID_PUBLIC_KEY = 5;\n    public static final int ERROR_MISSING_PERMISSION = 6;\n\n    /**\n     * Error in application code. Caller did not call or set up license checker\n     * correctly. Should be considered fatal.\n     */\n    public void applicationError(int errorCode);\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/LicenseValidator.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\nimport com.google.android.vending.licensing.util.Base64;\nimport com.google.android.vending.licensing.util.Base64DecoderException;\n\nimport android.text.TextUtils;\nimport android.util.Log;\n\nimport java.security.InvalidKeyException;\nimport java.security.NoSuchAlgorithmException;\nimport java.security.PublicKey;\nimport java.security.Signature;\nimport java.security.SignatureException;\n\n/**\n * Contains data related to a licensing request and methods to verify\n * and process the response.\n */\nclass LicenseValidator {\n    private static final String TAG = \"LicenseValidator\";\n\n    // Server response codes.\n    private static final int LICENSED = 0x0;\n    private static final int NOT_LICENSED = 0x1;\n    private static final int LICENSED_OLD_KEY = 0x2;\n    private static final int ERROR_NOT_MARKET_MANAGED = 0x3;\n    private static final int ERROR_SERVER_FAILURE = 0x4;\n    private static final int ERROR_OVER_QUOTA = 0x5;\n\n    private static final int ERROR_CONTACTING_SERVER = 0x101;\n    private static final int ERROR_INVALID_PACKAGE_NAME = 0x102;\n    private static final int ERROR_NON_MATCHING_UID = 0x103;\n\n    private final Policy mPolicy;\n    private final LicenseCheckerCallback mCallback;\n    private final int mNonce;\n    private final String mPackageName;\n    private final String mVersionCode;\n    private final DeviceLimiter mDeviceLimiter;\n\n    LicenseValidator(Policy policy, DeviceLimiter deviceLimiter, LicenseCheckerCallback callback,\n             int nonce, String packageName, String versionCode) {\n        mPolicy = policy;\n        mDeviceLimiter = deviceLimiter;\n        mCallback = callback;\n        mNonce = nonce;\n        mPackageName = packageName;\n        mVersionCode = versionCode;\n    }\n\n    public LicenseCheckerCallback getCallback() {\n        return mCallback;\n    }\n\n    public int getNonce() {\n        return mNonce;\n    }\n\n    public String getPackageName() {\n        return mPackageName;\n    }\n\n    private static final String SIGNATURE_ALGORITHM = \"SHA1withRSA\";\n\n    /**\n     * Verifies the response from server and calls appropriate callback method.\n     *\n     * @param publicKey public key associated with the developer account\n     * @param responseCode server response code\n     * @param signedData signed data from server\n     * @param signature server signature\n     */\n    public void verify(PublicKey publicKey, int responseCode, String signedData, String signature) {\n        String userId = null;\n        // Skip signature check for unsuccessful requests\n        ResponseData data = null;\n        if (responseCode == LICENSED || responseCode == NOT_LICENSED ||\n                responseCode == LICENSED_OLD_KEY) {\n            // Verify signature.\n            try {\n                if (TextUtils.isEmpty(signedData)) {\n                    Log.e(TAG, \"Signature verification failed: signedData is empty. \" +\n                            \"(Device not signed-in to any Google accounts?)\");\n                    handleInvalidResponse();\n                    return;\n                }\n\n                Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM);\n                sig.initVerify(publicKey);\n                sig.update(signedData.getBytes());\n\n                if (!sig.verify(Base64.decode(signature))) {\n                    Log.e(TAG, \"Signature verification failed.\");\n                    handleInvalidResponse();\n                    return;\n                }\n            } catch (NoSuchAlgorithmException e) {\n                // This can't happen on an Android compatible device.\n                throw new RuntimeException(e);\n            } catch (InvalidKeyException e) {\n                handleApplicationError(LicenseCheckerCallback.ERROR_INVALID_PUBLIC_KEY);\n                return;\n            } catch (SignatureException e) {\n                throw new RuntimeException(e);\n            } catch (Base64DecoderException e) {\n                Log.e(TAG, \"Could not Base64-decode signature.\");\n                handleInvalidResponse();\n                return;\n            }\n\n            // Parse and validate response.\n            try {\n                data = ResponseData.parse(signedData);\n            } catch (IllegalArgumentException e) {\n                Log.e(TAG, \"Could not parse response.\");\n                handleInvalidResponse();\n                return;\n            }\n\n            if (data.responseCode != responseCode) {\n                Log.e(TAG, \"Response codes don't match.\");\n                handleInvalidResponse();\n                return;\n            }\n\n            if (data.nonce != mNonce) {\n                Log.e(TAG, \"Nonce doesn't match.\");\n                handleInvalidResponse();\n                return;\n            }\n\n            if (!data.packageName.equals(mPackageName)) {\n                Log.e(TAG, \"Package name doesn't match.\");\n                handleInvalidResponse();\n                return;\n            }\n\n            if (!data.versionCode.equals(mVersionCode)) {\n                Log.e(TAG, \"Version codes don't match.\");\n                handleInvalidResponse();\n                return;\n            }\n\n            // Application-specific user identifier.\n            userId = data.userId;\n            if (TextUtils.isEmpty(userId)) {\n                Log.e(TAG, \"User identifier is empty.\");\n                handleInvalidResponse();\n                return;\n            }\n        }\n\n        switch (responseCode) {\n            case LICENSED:\n            case LICENSED_OLD_KEY:\n                int limiterResponse = mDeviceLimiter.isDeviceAllowed(userId);\n                handleResponse(limiterResponse, data);\n                break;\n            case NOT_LICENSED:\n                handleResponse(Policy.NOT_LICENSED, data);\n                break;\n            case ERROR_CONTACTING_SERVER:\n                Log.w(TAG, \"Error contacting licensing server.\");\n                handleResponse(Policy.RETRY, data);\n                break;\n            case ERROR_SERVER_FAILURE:\n                Log.w(TAG, \"An error has occurred on the licensing server.\");\n                handleResponse(Policy.RETRY, data);\n                break;\n            case ERROR_OVER_QUOTA:\n                Log.w(TAG, \"Licensing server is refusing to talk to this device, over quota.\");\n                handleResponse(Policy.RETRY, data);\n                break;\n            case ERROR_INVALID_PACKAGE_NAME:\n                handleApplicationError(LicenseCheckerCallback.ERROR_INVALID_PACKAGE_NAME);\n                break;\n            case ERROR_NON_MATCHING_UID:\n                handleApplicationError(LicenseCheckerCallback.ERROR_NON_MATCHING_UID);\n                break;\n            case ERROR_NOT_MARKET_MANAGED:\n                handleApplicationError(LicenseCheckerCallback.ERROR_NOT_MARKET_MANAGED);\n                break;\n            default:\n                Log.e(TAG, \"Unknown response code for license check.\");\n                handleInvalidResponse();\n        }\n    }\n\n    /**\n     * Confers with policy and calls appropriate callback method.\n     *\n     * @param response\n     * @param rawData\n     */\n    private void handleResponse(int response, ResponseData rawData) {\n        // Update policy data and increment retry counter (if needed)\n        mPolicy.processServerResponse(response, rawData);\n\n        // Given everything we know, including cached data, ask the policy if we should grant\n        // access.\n        if (mPolicy.allowAccess()) {\n            mCallback.allow(response);\n        } else {\n            mCallback.dontAllow(response);\n        }\n    }\n\n    private void handleApplicationError(int code) {\n        mCallback.applicationError(code);\n    }\n\n    private void handleInvalidResponse() {\n        mCallback.dontAllow(Policy.NOT_LICENSED);\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/NullDeviceLimiter.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\n/**\n * A DeviceLimiter that doesn't limit the number of devices that can use a\n * given user's license.\n * <p>\n * Unless you have reason to believe that your application is being pirated\n * by multiple users using the same license (signing in to Market as the same\n * user), we recommend you use this implementation.\n */\npublic class NullDeviceLimiter implements DeviceLimiter {\n\n    public int isDeviceAllowed(String userId) {\n        return Policy.LICENSED;\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/Obfuscator.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\n/**\n * Interface used as part of a {@link Policy} to allow application authors to obfuscate\n * licensing data that will be stored into a SharedPreferences file.\n * <p>\n * Any transformation scheme must be reversable. Implementing classes may optionally implement an\n * integrity check to further prevent modification to preference data. Implementing classes\n * should use device-specific information as a key in the obfuscation algorithm to prevent\n * obfuscated preferences from being shared among devices.\n */\npublic interface Obfuscator {\n\n    /**\n     * Obfuscate a string that is being stored into shared preferences.\n     *\n     * @param original The data that is to be obfuscated.\n     * @param key The key for the data that is to be obfuscated.\n     * @return A transformed version of the original data.\n     */\n    String obfuscate(String original, String key);\n\n    /**\n     * Undo the transformation applied to data by the obfuscate() method.\n     *\n     * @param original The data that is to be obfuscated.\n     * @param key The key for the data that is to be obfuscated.\n     * @return A transformed version of the original data.\n     * @throws ValidationException Optionally thrown if a data integrity check fails.\n     */\n    String unobfuscate(String obfuscated, String key) throws ValidationException;\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/Policy.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\n/**\n * Policy used by {@link LicenseChecker} to determine whether a user should have\n * access to the application.\n */\npublic interface Policy {\n\n    /**\n     * Change these values to make it more difficult for tools to automatically\n     * strip LVL protection from your APK.\n     */\n\n    /**\n     * LICENSED means that the server returned back a valid license response\n     */\n    public static final int LICENSED = 0x0100;\n    /**\n     * NOT_LICENSED means that the server returned back a valid license response\n     * that indicated that the user definitively is not licensed\n     */\n    public static final int NOT_LICENSED = 0x0231;\n    /**\n     * RETRY means that the license response was unable to be determined ---\n     * perhaps as a result of faulty networking\n     */\n    public static final int RETRY = 0x0123;\n\n    /**\n     * Provide results from contact with the license server. Retry counts are\n     * incremented if the current value of response is RETRY. Results will be\n     * used for any future policy decisions.\n     * \n     * @param response the result from validating the server response\n     * @param rawData the raw server response data, can be null for RETRY\n     */\n    void processServerResponse(int response, ResponseData rawData);\n\n    /**\n     * Check if the user should be allowed access to the application.\n     */\n    boolean allowAccess();\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/PreferenceObfuscator.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\nimport android.content.SharedPreferences;\nimport android.util.Log;\n\n/**\n * An wrapper for SharedPreferences that transparently performs data obfuscation.\n */\npublic class PreferenceObfuscator {\n\n    private static final String TAG = \"PreferenceObfuscator\";\n\n    private final SharedPreferences mPreferences;\n    private final Obfuscator mObfuscator;\n    private SharedPreferences.Editor mEditor;\n\n    /**\n     * Constructor.\n     *\n     * @param sp A SharedPreferences instance provided by the system.\n     * @param o The Obfuscator to use when reading or writing data.\n     */\n    public PreferenceObfuscator(SharedPreferences sp, Obfuscator o) {\n        mPreferences = sp;\n        mObfuscator = o;\n        mEditor = null;\n    }\n\n    public void putString(String key, String value) {\n        if (mEditor == null) {\n            mEditor = mPreferences.edit();\n        }\n        String obfuscatedValue = mObfuscator.obfuscate(value, key);\n        mEditor.putString(key, obfuscatedValue);\n    }\n\n    public String getString(String key, String defValue) {\n        String result;\n        String value = mPreferences.getString(key, null);\n        if (value != null) {\n            try {\n                result = mObfuscator.unobfuscate(value, key);\n            } catch (ValidationException e) {\n                // Unable to unobfuscate, data corrupt or tampered\n                Log.w(TAG, \"Validation error while reading preference: \" + key);\n                result = defValue;\n            }\n        } else {\n            // Preference not found\n            result = defValue;\n        }\n        return result;\n    }\n\n    public void commit() {\n        if (mEditor != null) {\n            mEditor.commit();\n            mEditor = null;\n        }\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/ResponseData.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\nimport android.text.TextUtils;\n\nimport java.util.regex.Pattern;\n\n/**\n * ResponseData from licensing server.\n */\npublic class ResponseData {\n\n    public int responseCode;\n    public int nonce;\n    public String packageName;\n    public String versionCode;\n    public String userId;\n    public long timestamp;\n    /** Response-specific data. */\n    public String extra;\n\n    /**\n     * Parses response string into ResponseData.\n     *\n     * @param responseData response data string\n     * @throws IllegalArgumentException upon parsing error\n     * @return ResponseData object\n     */\n    public static ResponseData parse(String responseData) {\n        // Must parse out main response data and response-specific data.\n        int index = responseData.indexOf(':');\n        String mainData, extraData;\n        if (-1 == index) {\n            mainData = responseData;\n            extraData = \"\";\n        } else {\n            mainData = responseData.substring(0, index);\n            extraData = index >= responseData.length() ? \"\" : responseData.substring(index + 1);\n        }\n\n        String[] fields = TextUtils.split(mainData, Pattern.quote(\"|\"));\n        if (fields.length < 6) {\n            throw new IllegalArgumentException(\"Wrong number of fields.\");\n        }\n\n        ResponseData data = new ResponseData();\n        data.extra = extraData;\n        data.responseCode = Integer.parseInt(fields[0]);\n        data.nonce = Integer.parseInt(fields[1]);\n        data.packageName = fields[2];\n        data.versionCode = fields[3];\n        // Application-specific user identifier.\n        data.userId = fields[4];\n        data.timestamp = Long.parseLong(fields[5]);\n\n        return data;\n    }\n\n    @Override\n    public String toString() {\n        return TextUtils.join(\"|\", new Object[] {\n                responseCode, nonce, packageName, versionCode,\n                userId, timestamp\n        });\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/ServerManagedPolicy.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\nimport java.net.URI;\nimport java.net.URISyntaxException;\nimport java.util.HashMap;\nimport java.util.Map;\n\nimport android.content.Context;\nimport android.content.SharedPreferences;\nimport android.util.Log;\n\nimport com.google.android.vending.licensing.util.URIQueryDecoder;\n\n/**\n * Default policy. All policy decisions are based off of response data received\n * from the licensing service. Specifically, the licensing server sends the\n * following information: response validity period, error retry period, and\n * error retry count.\n * <p>\n * These values will vary based on the the way the application is configured in\n * the Google Play publishing console, such as whether the application is\n * marked as free or is within its refund period, as well as how often an\n * application is checking with the licensing service.\n * <p>\n * Developers who need more fine grained control over their application's\n * licensing policy should implement a custom Policy.\n */\npublic class ServerManagedPolicy implements Policy {\n\n    private static final String TAG = \"ServerManagedPolicy\";\n    private static final String PREFS_FILE = \"com.google.android.vending.licensing.ServerManagedPolicy\";\n    private static final String PREF_LAST_RESPONSE = \"lastResponse\";\n    private static final String PREF_VALIDITY_TIMESTAMP = \"validityTimestamp\";\n    private static final String PREF_RETRY_UNTIL = \"retryUntil\";\n    private static final String PREF_MAX_RETRIES = \"maxRetries\";\n    private static final String PREF_RETRY_COUNT = \"retryCount\";\n    private static final String DEFAULT_VALIDITY_TIMESTAMP = \"0\";\n    private static final String DEFAULT_RETRY_UNTIL = \"0\";\n    private static final String DEFAULT_MAX_RETRIES = \"0\";\n    private static final String DEFAULT_RETRY_COUNT = \"0\";\n\n    private static final long MILLIS_PER_MINUTE = 60 * 1000;\n\n    private long mValidityTimestamp;\n    private long mRetryUntil;\n    private long mMaxRetries;\n    private long mRetryCount;\n    private long mLastResponseTime = 0;\n    private int mLastResponse;\n    private PreferenceObfuscator mPreferences;\n\n    /**\n     * @param context The context for the current application\n     * @param obfuscator An obfuscator to be used with preferences.\n     */\n    public ServerManagedPolicy(Context context, Obfuscator obfuscator) {\n        // Import old values\n        SharedPreferences sp = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);\n        mPreferences = new PreferenceObfuscator(sp, obfuscator);\n        mLastResponse = Integer.parseInt(\n            mPreferences.getString(PREF_LAST_RESPONSE, Integer.toString(Policy.RETRY)));\n        mValidityTimestamp = Long.parseLong(mPreferences.getString(PREF_VALIDITY_TIMESTAMP,\n                DEFAULT_VALIDITY_TIMESTAMP));\n        mRetryUntil = Long.parseLong(mPreferences.getString(PREF_RETRY_UNTIL, DEFAULT_RETRY_UNTIL));\n        mMaxRetries = Long.parseLong(mPreferences.getString(PREF_MAX_RETRIES, DEFAULT_MAX_RETRIES));\n        mRetryCount = Long.parseLong(mPreferences.getString(PREF_RETRY_COUNT, DEFAULT_RETRY_COUNT));\n    }\n\n    /**\n     * Process a new response from the license server.\n     * <p>\n     * This data will be used for computing future policy decisions. The\n     * following parameters are processed:\n     * <ul>\n     * <li>VT: the timestamp that the client should consider the response\n     *   valid until\n     * <li>GT: the timestamp that the client should ignore retry errors until\n     * <li>GR: the number of retry errors that the client should ignore\n     * </ul>\n     *\n     * @param response the result from validating the server response\n     * @param rawData the raw server response data\n     */\n    public void processServerResponse(int response, ResponseData rawData) {\n\n        // Update retry counter\n        if (response != Policy.RETRY) {\n            setRetryCount(0);\n        } else {\n            setRetryCount(mRetryCount + 1);\n        }\n\n        if (response == Policy.LICENSED) {\n            // Update server policy data\n            Map<String, String> extras = decodeExtras(rawData.extra);\n            mLastResponse = response;\n            setValidityTimestamp(extras.get(\"VT\"));\n            setRetryUntil(extras.get(\"GT\"));\n            setMaxRetries(extras.get(\"GR\"));\n        } else if (response == Policy.NOT_LICENSED) {\n            // Clear out stale policy data\n            setValidityTimestamp(DEFAULT_VALIDITY_TIMESTAMP);\n            setRetryUntil(DEFAULT_RETRY_UNTIL);\n            setMaxRetries(DEFAULT_MAX_RETRIES);\n        }\n\n        setLastResponse(response);\n        mPreferences.commit();\n    }\n\n    /**\n     * Set the last license response received from the server and add to\n     * preferences. You must manually call PreferenceObfuscator.commit() to\n     * commit these changes to disk.\n     *\n     * @param l the response\n     */\n    private void setLastResponse(int l) {\n        mLastResponseTime = System.currentTimeMillis();\n        mLastResponse = l;\n        mPreferences.putString(PREF_LAST_RESPONSE, Integer.toString(l));\n    }\n\n    /**\n     * Set the current retry count and add to preferences. You must manually\n     * call PreferenceObfuscator.commit() to commit these changes to disk.\n     *\n     * @param c the new retry count\n     */\n    private void setRetryCount(long c) {\n        mRetryCount = c;\n        mPreferences.putString(PREF_RETRY_COUNT, Long.toString(c));\n    }\n\n    public long getRetryCount() {\n        return mRetryCount;\n    }\n\n    /**\n     * Set the last validity timestamp (VT) received from the server and add to\n     * preferences. You must manually call PreferenceObfuscator.commit() to\n     * commit these changes to disk.\n     *\n     * @param validityTimestamp the VT string received\n     */\n    private void setValidityTimestamp(String validityTimestamp) {\n        Long lValidityTimestamp;\n        try {\n            lValidityTimestamp = Long.parseLong(validityTimestamp);\n        } catch (NumberFormatException e) {\n            // No response or not parsable, expire in one minute.\n            Log.w(TAG, \"License validity timestamp (VT) missing, caching for a minute\");\n            lValidityTimestamp = System.currentTimeMillis() + MILLIS_PER_MINUTE;\n            validityTimestamp = Long.toString(lValidityTimestamp);\n        }\n\n        mValidityTimestamp = lValidityTimestamp;\n        mPreferences.putString(PREF_VALIDITY_TIMESTAMP, validityTimestamp);\n    }\n\n    public long getValidityTimestamp() {\n        return mValidityTimestamp;\n    }\n\n    /**\n     * Set the retry until timestamp (GT) received from the server and add to\n     * preferences. You must manually call PreferenceObfuscator.commit() to\n     * commit these changes to disk.\n     *\n     * @param retryUntil the GT string received\n     */\n    private void setRetryUntil(String retryUntil) {\n        Long lRetryUntil;\n        try {\n            lRetryUntil = Long.parseLong(retryUntil);\n        } catch (NumberFormatException e) {\n            // No response or not parsable, expire immediately\n            Log.w(TAG, \"License retry timestamp (GT) missing, grace period disabled\");\n            retryUntil = \"0\";\n            lRetryUntil = 0l;\n        }\n\n        mRetryUntil = lRetryUntil;\n        mPreferences.putString(PREF_RETRY_UNTIL, retryUntil);\n    }\n\n    public long getRetryUntil() {\n      return mRetryUntil;\n    }\n\n    /**\n     * Set the max retries value (GR) as received from the server and add to\n     * preferences. You must manually call PreferenceObfuscator.commit() to\n     * commit these changes to disk.\n     *\n     * @param maxRetries the GR string received\n     */\n    private void setMaxRetries(String maxRetries) {\n        Long lMaxRetries;\n        try {\n            lMaxRetries = Long.parseLong(maxRetries);\n        } catch (NumberFormatException e) {\n            // No response or not parsable, expire immediately\n            Log.w(TAG, \"Licence retry count (GR) missing, grace period disabled\");\n            maxRetries = \"0\";\n            lMaxRetries = 0l;\n        }\n\n        mMaxRetries = lMaxRetries;\n        mPreferences.putString(PREF_MAX_RETRIES, maxRetries);\n    }\n\n    public long getMaxRetries() {\n        return mMaxRetries;\n    }\n\n    /**\n     * {@inheritDoc}\n     *\n     * This implementation allows access if either:<br>\n     * <ol>\n     * <li>a LICENSED response was received within the validity period\n     * <li>a RETRY response was received in the last minute, and we are under\n     * the RETRY count or in the RETRY period.\n     * </ol>\n     */\n    public boolean allowAccess() {\n        long ts = System.currentTimeMillis();\n        if (mLastResponse == Policy.LICENSED) {\n            // Check if the LICENSED response occurred within the validity timeout.\n            if (ts <= mValidityTimestamp) {\n                // Cached LICENSED response is still valid.\n                return true;\n            }\n        } else if (mLastResponse == Policy.RETRY &&\n                   ts < mLastResponseTime + MILLIS_PER_MINUTE) {\n            // Only allow access if we are within the retry period or we haven't used up our\n            // max retries.\n            return (ts <= mRetryUntil || mRetryCount <= mMaxRetries);\n        }\n        return false;\n    }\n\n    private Map<String, String> decodeExtras(String extras) {\n        Map<String, String> results = new HashMap<String, String>();\n        try {\n            URI rawExtras = new URI(\"?\" + extras);\n            URIQueryDecoder.DecodeQuery(rawExtras, results);\n        } catch (URISyntaxException e) {\n            Log.w(TAG, \"Invalid syntax error while decoding extras data from server.\");\n        }\n        return results;\n    }\n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/StrictPolicy.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\n/**\n * Non-caching policy. All requests will be sent to the licensing service,\n * and no local caching is performed.\n * <p>\n * Using a non-caching policy ensures that there is no local preference data\n * for malicious users to tamper with. As a side effect, applications\n * will not be permitted to run while offline. Developers should carefully\n * weigh the risks of using this Policy over one which implements caching,\n * such as ServerManagedPolicy.\n * <p>\n * Access to the application is only allowed if a LICESNED response is.\n * received. All other responses (including RETRY) will deny access.\n */\npublic class StrictPolicy implements Policy {\n\n    private int mLastResponse;\n\n    public StrictPolicy() {\n        // Set default policy. This will force the application to check the policy on launch.\n        mLastResponse = Policy.RETRY;\n    }\n\n    /**\n     * Process a new response from the license server. Since we aren't\n     * performing any caching, this equates to reading the LicenseResponse.\n     * Any ResponseData provided is ignored.\n     *\n     * @param response the result from validating the server response\n     * @param rawData the raw server response data\n     */\n    public void processServerResponse(int response, ResponseData rawData) {\n        mLastResponse = response;\n    }\n\n    /**\n     * {@inheritDoc}\n     *\n     * This implementation allows access if and only if a LICENSED response\n     * was received the last time the server was contacted.\n     */\n    public boolean allowAccess() {\n        return (mLastResponse == Policy.LICENSED);\n    }\n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/ValidationException.java",
    "content": "/*\n * Copyright (C) 2010 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing;\n\n/**\n * Indicates that an error occurred while validating the integrity of data managed by an\n * {@link Obfuscator}.}\n */\npublic class ValidationException extends Exception {\n    public ValidationException() {\n      super();\n    }\n\n    public ValidationException(String s) {\n      super(s);\n    }\n\n    private static final long serialVersionUID = 1L;\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/util/Base64.java",
    "content": "// Portions copyright 2002, Google, Inc.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n//     http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\npackage com.google.android.vending.licensing.util;\n\n// This code was converted from code at http://iharder.sourceforge.net/base64/\n// Lots of extraneous features were removed.\n/* The original code said:\n * <p>\n * I am placing this code in the Public Domain. Do with it as you will.\n * This software comes with no guarantees or warranties but with\n * plenty of well-wishing instead!\n * Please visit\n * <a href=\"http://iharder.net/xmlizable\">http://iharder.net/xmlizable</a>\n * periodically to check for updates or to contribute improvements.\n * </p>\n *\n * @author Robert Harder\n * @author rharder@usa.net\n * @version 1.3\n */\n\n/**\n * Base64 converter class. This code is not a full-blown MIME encoder;\n * it simply converts binary data to base64 data and back.\n *\n * <p>Note {@link CharBase64} is a GWT-compatible implementation of this\n * class.\n */\npublic class Base64 {\n  /** Specify encoding (value is {@code true}). */\n  public final static boolean ENCODE = true;\n\n  /** Specify decoding (value is {@code false}). */\n  public final static boolean DECODE = false;\n\n  /** The equals sign (=) as a byte. */\n  private final static byte EQUALS_SIGN = (byte) '=';\n\n  /** The new line character (\\n) as a byte. */\n  private final static byte NEW_LINE = (byte) '\\n';\n\n  /**\n   * The 64 valid Base64 values.\n   */\n  private final static byte[] ALPHABET =\n      {(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F',\n          (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K',\n          (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P',\n          (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U',\n          (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z',\n          (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e',\n          (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j',\n          (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o',\n          (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't',\n          (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y',\n          (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3',\n          (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8',\n          (byte) '9', (byte) '+', (byte) '/'};\n\n  /**\n   * The 64 valid web safe Base64 values.\n   */\n  private final static byte[] WEBSAFE_ALPHABET =\n      {(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F',\n          (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K',\n          (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P',\n          (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U',\n          (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z',\n          (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e',\n          (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j',\n          (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o',\n          (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't',\n          (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y',\n          (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3',\n          (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8',\n          (byte) '9', (byte) '-', (byte) '_'};\n\n  /**\n   * Translates a Base64 value to either its 6-bit reconstruction value\n   * or a negative number indicating some other meaning.\n   **/\n  private final static byte[] DECODABET = {-9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal  0 -  8\n      -5, -5, // Whitespace: Tab and Linefeed\n      -9, -9, // Decimal 11 - 12\n      -5, // Whitespace: Carriage Return\n      -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26\n      -9, -9, -9, -9, -9, // Decimal 27 - 31\n      -5, // Whitespace: Space\n      -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42\n      62, // Plus sign at decimal 43\n      -9, -9, -9, // Decimal 44 - 46\n      63, // Slash at decimal 47\n      52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine\n      -9, -9, -9, // Decimal 58 - 60\n      -1, // Equals sign at decimal 61\n      -9, -9, -9, // Decimal 62 - 64\n      0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' through 'N'\n      14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' through 'Z'\n      -9, -9, -9, -9, -9, -9, // Decimal 91 - 96\n      26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'\n      39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'\n      -9, -9, -9, -9, -9 // Decimal 123 - 127\n      /*  ,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 128 - 139\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 140 - 152\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 153 - 165\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 166 - 178\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 179 - 191\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 192 - 204\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 205 - 217\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 218 - 230\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 231 - 243\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9         // Decimal 244 - 255 */\n      };\n\n  /** The web safe decodabet */\n  private final static byte[] WEBSAFE_DECODABET =\n      {-9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal  0 -  8\n          -5, -5, // Whitespace: Tab and Linefeed\n          -9, -9, // Decimal 11 - 12\n          -5, // Whitespace: Carriage Return\n          -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26\n          -9, -9, -9, -9, -9, // Decimal 27 - 31\n          -5, // Whitespace: Space\n          -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 44\n          62, // Dash '-' sign at decimal 45\n          -9, -9, // Decimal 46-47\n          52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine\n          -9, -9, -9, // Decimal 58 - 60\n          -1, // Equals sign at decimal 61\n          -9, -9, -9, // Decimal 62 - 64\n          0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' through 'N'\n          14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' through 'Z'\n          -9, -9, -9, -9, // Decimal 91-94\n          63, // Underscore '_' at decimal 95\n          -9, // Decimal 96\n          26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'\n          39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'\n          -9, -9, -9, -9, -9 // Decimal 123 - 127\n      /*  ,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 128 - 139\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 140 - 152\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 153 - 165\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 166 - 178\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 179 - 191\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 192 - 204\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 205 - 217\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 218 - 230\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 231 - 243\n        -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9         // Decimal 244 - 255 */\n      };\n\n  // Indicates white space in encoding\n  private final static byte WHITE_SPACE_ENC = -5;\n  // Indicates equals sign in encoding\n  private final static byte EQUALS_SIGN_ENC = -1;\n\n  /** Defeats instantiation. */\n  private Base64() {\n  }\n\n  /* ********  E N C O D I N G   M E T H O D S  ******** */\n\n  /**\n   * Encodes up to three bytes of the array <var>source</var>\n   * and writes the resulting four Base64 bytes to <var>destination</var>.\n   * The source and destination arrays can be manipulated\n   * anywhere along their length by specifying\n   * <var>srcOffset</var> and <var>destOffset</var>.\n   * This method does not check to make sure your arrays\n   * are large enough to accommodate <var>srcOffset</var> + 3 for\n   * the <var>source</var> array or <var>destOffset</var> + 4 for\n   * the <var>destination</var> array.\n   * The actual number of significant bytes in your array is\n   * given by <var>numSigBytes</var>.\n   *\n   * @param source the array to convert\n   * @param srcOffset the index where conversion begins\n   * @param numSigBytes the number of significant bytes in your array\n   * @param destination the array to hold the conversion\n   * @param destOffset the index where output will be put\n   * @param alphabet is the encoding alphabet\n   * @return the <var>destination</var> array\n   * @since 1.3\n   */\n  private static byte[] encode3to4(byte[] source, int srcOffset,\n      int numSigBytes, byte[] destination, int destOffset, byte[] alphabet) {\n    //           1         2         3\n    // 01234567890123456789012345678901 Bit position\n    // --------000000001111111122222222 Array position from threeBytes\n    // --------|    ||    ||    ||    | Six bit groups to index alphabet\n    //          >>18  >>12  >> 6  >> 0  Right shift necessary\n    //                0x3f  0x3f  0x3f  Additional AND\n\n    // Create buffer with zero-padding if there are only one or two\n    // significant bytes passed in the array.\n    // We have to shift left 24 in order to flush out the 1's that appear\n    // when Java treats a value as negative that is cast from a byte to an int.\n    int inBuff =\n        (numSigBytes > 0 ? ((source[srcOffset] << 24) >>> 8) : 0)\n            | (numSigBytes > 1 ? ((source[srcOffset + 1] << 24) >>> 16) : 0)\n            | (numSigBytes > 2 ? ((source[srcOffset + 2] << 24) >>> 24) : 0);\n\n    switch (numSigBytes) {\n      case 3:\n        destination[destOffset] = alphabet[(inBuff >>> 18)];\n        destination[destOffset + 1] = alphabet[(inBuff >>> 12) & 0x3f];\n        destination[destOffset + 2] = alphabet[(inBuff >>> 6) & 0x3f];\n        destination[destOffset + 3] = alphabet[(inBuff) & 0x3f];\n        return destination;\n      case 2:\n        destination[destOffset] = alphabet[(inBuff >>> 18)];\n        destination[destOffset + 1] = alphabet[(inBuff >>> 12) & 0x3f];\n        destination[destOffset + 2] = alphabet[(inBuff >>> 6) & 0x3f];\n        destination[destOffset + 3] = EQUALS_SIGN;\n        return destination;\n      case 1:\n        destination[destOffset] = alphabet[(inBuff >>> 18)];\n        destination[destOffset + 1] = alphabet[(inBuff >>> 12) & 0x3f];\n        destination[destOffset + 2] = EQUALS_SIGN;\n        destination[destOffset + 3] = EQUALS_SIGN;\n        return destination;\n      default:\n        return destination;\n    } // end switch\n  } // end encode3to4\n\n  /**\n   * Encodes a byte array into Base64 notation.\n   * Equivalent to calling\n   * {@code encodeBytes(source, 0, source.length)}\n   *\n   * @param source The data to convert\n   * @since 1.4\n   */\n  public static String encode(byte[] source) {\n    return encode(source, 0, source.length, ALPHABET, true);\n  }\n\n  /**\n   * Encodes a byte array into web safe Base64 notation.\n   *\n   * @param source The data to convert\n   * @param doPadding is {@code true} to pad result with '=' chars\n   *        if it does not fall on 3 byte boundaries\n   */\n  public static String encodeWebSafe(byte[] source, boolean doPadding) {\n    return encode(source, 0, source.length, WEBSAFE_ALPHABET, doPadding);\n  }\n\n  /**\n   * Encodes a byte array into Base64 notation.\n   *\n   * @param source The data to convert\n   * @param off Offset in array where conversion should begin\n   * @param len Length of data to convert\n   * @param alphabet is the encoding alphabet\n   * @param doPadding is {@code true} to pad result with '=' chars\n   *        if it does not fall on 3 byte boundaries\n   * @since 1.4\n   */\n  public static String encode(byte[] source, int off, int len, byte[] alphabet,\n      boolean doPadding) {\n    byte[] outBuff = encode(source, off, len, alphabet, Integer.MAX_VALUE);\n    int outLen = outBuff.length;\n\n    // If doPadding is false, set length to truncate '='\n    // padding characters\n    while (doPadding == false && outLen > 0) {\n      if (outBuff[outLen - 1] != '=') {\n        break;\n      }\n      outLen -= 1;\n    }\n\n    return new String(outBuff, 0, outLen);\n  }\n\n  /**\n   * Encodes a byte array into Base64 notation.\n   *\n   * @param source The data to convert\n   * @param off Offset in array where conversion should begin\n   * @param len Length of data to convert\n   * @param alphabet is the encoding alphabet\n   * @param maxLineLength maximum length of one line.\n   * @return the BASE64-encoded byte array\n   */\n  public static byte[] encode(byte[] source, int off, int len, byte[] alphabet,\n      int maxLineLength) {\n    int lenDiv3 = (len + 2) / 3; // ceil(len / 3)\n    int len43 = lenDiv3 * 4;\n    byte[] outBuff = new byte[len43 // Main 4:3\n        + (len43 / maxLineLength)]; // New lines\n\n    int d = 0;\n    int e = 0;\n    int len2 = len - 2;\n    int lineLength = 0;\n    for (; d < len2; d += 3, e += 4) {\n\n      // The following block of code is the same as\n      // encode3to4( source, d + off, 3, outBuff, e, alphabet );\n      // but inlined for faster encoding (~20% improvement)\n      int inBuff =\n          ((source[d + off] << 24) >>> 8)\n              | ((source[d + 1 + off] << 24) >>> 16)\n              | ((source[d + 2 + off] << 24) >>> 24);\n      outBuff[e] = alphabet[(inBuff >>> 18)];\n      outBuff[e + 1] = alphabet[(inBuff >>> 12) & 0x3f];\n      outBuff[e + 2] = alphabet[(inBuff >>> 6) & 0x3f];\n      outBuff[e + 3] = alphabet[(inBuff) & 0x3f];\n\n      lineLength += 4;\n      if (lineLength == maxLineLength) {\n        outBuff[e + 4] = NEW_LINE;\n        e++;\n        lineLength = 0;\n      } // end if: end of line\n    } // end for: each piece of array\n\n    if (d < len) {\n      encode3to4(source, d + off, len - d, outBuff, e, alphabet);\n\n      lineLength += 4;\n      if (lineLength == maxLineLength) {\n        // Add a last newline\n        outBuff[e + 4] = NEW_LINE;\n        e++;\n      }\n      e += 4;\n    }\n\n    assert (e == outBuff.length);\n    return outBuff;\n  }\n\n\n  /* ********  D E C O D I N G   M E T H O D S  ******** */\n\n\n  /**\n   * Decodes four bytes from array <var>source</var>\n   * and writes the resulting bytes (up to three of them)\n   * to <var>destination</var>.\n   * The source and destination arrays can be manipulated\n   * anywhere along their length by specifying\n   * <var>srcOffset</var> and <var>destOffset</var>.\n   * This method does not check to make sure your arrays\n   * are large enough to accommodate <var>srcOffset</var> + 4 for\n   * the <var>source</var> array or <var>destOffset</var> + 3 for\n   * the <var>destination</var> array.\n   * This method returns the actual number of bytes that\n   * were converted from the Base64 encoding.\n   *\n   *\n   * @param source the array to convert\n   * @param srcOffset the index where conversion begins\n   * @param destination the array to hold the conversion\n   * @param destOffset the index where output will be put\n   * @param decodabet the decodabet for decoding Base64 content\n   * @return the number of decoded bytes converted\n   * @since 1.3\n   */\n  private static int decode4to3(byte[] source, int srcOffset,\n      byte[] destination, int destOffset, byte[] decodabet) {\n    // Example: Dk==\n    if (source[srcOffset + 2] == EQUALS_SIGN) {\n      int outBuff =\n          ((decodabet[source[srcOffset]] << 24) >>> 6)\n              | ((decodabet[source[srcOffset + 1]] << 24) >>> 12);\n\n      destination[destOffset] = (byte) (outBuff >>> 16);\n      return 1;\n    } else if (source[srcOffset + 3] == EQUALS_SIGN) {\n      // Example: DkL=\n      int outBuff =\n          ((decodabet[source[srcOffset]] << 24) >>> 6)\n              | ((decodabet[source[srcOffset + 1]] << 24) >>> 12)\n              | ((decodabet[source[srcOffset + 2]] << 24) >>> 18);\n\n      destination[destOffset] = (byte) (outBuff >>> 16);\n      destination[destOffset + 1] = (byte) (outBuff >>> 8);\n      return 2;\n    } else {\n      // Example: DkLE\n      int outBuff =\n          ((decodabet[source[srcOffset]] << 24) >>> 6)\n              | ((decodabet[source[srcOffset + 1]] << 24) >>> 12)\n              | ((decodabet[source[srcOffset + 2]] << 24) >>> 18)\n              | ((decodabet[source[srcOffset + 3]] << 24) >>> 24);\n\n      destination[destOffset] = (byte) (outBuff >> 16);\n      destination[destOffset + 1] = (byte) (outBuff >> 8);\n      destination[destOffset + 2] = (byte) (outBuff);\n      return 3;\n    }\n  } // end decodeToBytes\n\n\n  /**\n   * Decodes data from Base64 notation.\n   *\n   * @param s the string to decode (decoded in default encoding)\n   * @return the decoded data\n   * @since 1.4\n   */\n  public static byte[] decode(String s) throws Base64DecoderException {\n    byte[] bytes = s.getBytes();\n    return decode(bytes, 0, bytes.length);\n  }\n\n  /**\n   * Decodes data from web safe Base64 notation.\n   * Web safe encoding uses '-' instead of '+', '_' instead of '/'\n   *\n   * @param s the string to decode (decoded in default encoding)\n   * @return the decoded data\n   */\n  public static byte[] decodeWebSafe(String s) throws Base64DecoderException {\n    byte[] bytes = s.getBytes();\n    return decodeWebSafe(bytes, 0, bytes.length);\n  }\n\n  /**\n   * Decodes Base64 content in byte array format and returns\n   * the decoded byte array.\n   *\n   * @param source The Base64 encoded data\n   * @return decoded data\n   * @since 1.3\n   * @throws Base64DecoderException\n   */\n  public static byte[] decode(byte[] source) throws Base64DecoderException {\n    return decode(source, 0, source.length);\n  }\n\n  /**\n   * Decodes web safe Base64 content in byte array format and returns\n   * the decoded data.\n   * Web safe encoding uses '-' instead of '+', '_' instead of '/'\n   *\n   * @param source the string to decode (decoded in default encoding)\n   * @return the decoded data\n   */\n  public static byte[] decodeWebSafe(byte[] source)\n      throws Base64DecoderException {\n    return decodeWebSafe(source, 0, source.length);\n  }\n\n  /**\n   * Decodes Base64 content in byte array format and returns\n   * the decoded byte array.\n   *\n   * @param source The Base64 encoded data\n   * @param off    The offset of where to begin decoding\n   * @param len    The length of characters to decode\n   * @return decoded data\n   * @since 1.3\n   * @throws Base64DecoderException\n   */\n  public static byte[] decode(byte[] source, int off, int len)\n      throws Base64DecoderException {\n    return decode(source, off, len, DECODABET);\n  }\n\n  /**\n   * Decodes web safe Base64 content in byte array format and returns\n   * the decoded byte array.\n   * Web safe encoding uses '-' instead of '+', '_' instead of '/'\n   *\n   * @param source The Base64 encoded data\n   * @param off    The offset of where to begin decoding\n   * @param len    The length of characters to decode\n   * @return decoded data\n   */\n  public static byte[] decodeWebSafe(byte[] source, int off, int len)\n      throws Base64DecoderException {\n    return decode(source, off, len, WEBSAFE_DECODABET);\n  }\n\n  /**\n   * Decodes Base64 content using the supplied decodabet and returns\n   * the decoded byte array.\n   *\n   * @param source    The Base64 encoded data\n   * @param off       The offset of where to begin decoding\n   * @param len       The length of characters to decode\n   * @param decodabet the decodabet for decoding Base64 content\n   * @return decoded data\n   */\n  public static byte[] decode(byte[] source, int off, int len, byte[] decodabet)\n      throws Base64DecoderException {\n    int len34 = len * 3 / 4;\n    byte[] outBuff = new byte[2 + len34]; // Upper limit on size of output\n    int outBuffPosn = 0;\n\n    byte[] b4 = new byte[4];\n    int b4Posn = 0;\n    int i = 0;\n    byte sbiCrop = 0;\n    byte sbiDecode = 0;\n    for (i = 0; i < len; i++) {\n      sbiCrop = (byte) (source[i + off] & 0x7f); // Only the low seven bits\n      sbiDecode = decodabet[sbiCrop];\n\n      if (sbiDecode >= WHITE_SPACE_ENC) { // White space Equals sign or better\n        if (sbiDecode >= EQUALS_SIGN_ENC) {\n          // An equals sign (for padding) must not occur at position 0 or 1\n          // and must be the last byte[s] in the encoded value\n          if (sbiCrop == EQUALS_SIGN) {\n            int bytesLeft = len - i;\n            byte lastByte = (byte) (source[len - 1 + off] & 0x7f);\n            if (b4Posn == 0 || b4Posn == 1) {\n              throw new Base64DecoderException(\n                  \"invalid padding byte '=' at byte offset \" + i);\n            } else if ((b4Posn == 3 && bytesLeft > 2)\n                || (b4Posn == 4 && bytesLeft > 1)) {\n              throw new Base64DecoderException(\n                  \"padding byte '=' falsely signals end of encoded value \"\n                      + \"at offset \" + i);\n            } else if (lastByte != EQUALS_SIGN && lastByte != NEW_LINE) {\n              throw new Base64DecoderException(\n                  \"encoded value has invalid trailing byte\");\n            }\n            break;\n          }\n\n          b4[b4Posn++] = sbiCrop;\n          if (b4Posn == 4) {\n            outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn, decodabet);\n            b4Posn = 0;\n          }\n        }\n      } else {\n        throw new Base64DecoderException(\"Bad Base64 input character at \" + i\n            + \": \" + source[i + off] + \"(decimal)\");\n      }\n    }\n\n    // Because web safe encoding allows non padding base64 encodes, we\n    // need to pad the rest of the b4 buffer with equal signs when\n    // b4Posn != 0.  There can be at most 2 equal signs at the end of\n    // four characters, so the b4 buffer must have two or three\n    // characters.  This also catches the case where the input is\n    // padded with EQUALS_SIGN\n    if (b4Posn != 0) {\n      if (b4Posn == 1) {\n        throw new Base64DecoderException(\"single trailing character at offset \"\n            + (len - 1));\n      }\n      b4[b4Posn++] = EQUALS_SIGN;\n      outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn, decodabet);\n    }\n\n    byte[] out = new byte[outBuffPosn];\n    System.arraycopy(outBuff, 0, out, 0, outBuffPosn);\n    return out;\n  }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/util/Base64DecoderException.java",
    "content": "// Copyright 2002, Google, Inc.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n//     http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\npackage com.google.android.vending.licensing.util;\n\n/**\n * Exception thrown when encountering an invalid Base64 input character.\n *\n * @author nelson\n */\npublic class Base64DecoderException extends Exception {\n  public Base64DecoderException() {\n    super();\n  }\n\n  public Base64DecoderException(String s) {\n    super(s);\n  }\n\n  private static final long serialVersionUID = 1L;\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/google/android/vending/licensing/util/URIQueryDecoder.java",
    "content": "/*\n * Copyright (C) 2016 The Android Open Source Project\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\npackage com.google.android.vending.licensing.util;\n\nimport android.util.Log;\n\nimport java.io.UnsupportedEncodingException;\nimport java.net.URI;\nimport java.net.URLDecoder;\nimport java.util.Map;\nimport java.util.Scanner;\n\npublic class URIQueryDecoder {\n    private static final String TAG = \"URIQueryDecoder\";\n\n    /**\n     * Decodes the query portion of the passed-in URI.\n     *\n     * @param encodedURI the URI containing the query to decode\n     * @param results a map containing all query parameters. Query parameters that do not have a\n     *            value will map to a null string\n     */\n    static public void DecodeQuery(URI encodedURI, Map<String, String> results) {\n        Scanner scanner = new Scanner(encodedURI.getRawQuery());\n        scanner.useDelimiter(\"&\");\n        try {\n            while (scanner.hasNext()) {\n                String param = scanner.next();\n                String[] valuePair = param.split(\"=\");\n                String name, value;\n                if (valuePair.length == 1) {\n                    value = null;\n                } else if (valuePair.length == 2) {\n                    value = URLDecoder.decode(valuePair[1], \"UTF-8\");\n                } else {\n                    throw new IllegalArgumentException(\"query parameter invalid\");\n                }\n                name = URLDecoder.decode(valuePair[0], \"UTF-8\");\n                results.put(name, value);\n            }\n        } catch (UnsupportedEncodingException e) {\n            // This should never happen.\n            Log.e(TAG, \"UTF-8 Not Recognized as a charset.  Device configuration Error.\");\n        }\n    }\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/unity3d/plugin/downloader/UnityAlarmReceiver.java",
    "content": "package com.unity3d.plugin.downloader;\n\nimport android.content.BroadcastReceiver;\nimport android.content.Context;\nimport android.content.Intent;\nimport android.content.pm.PackageManager.NameNotFoundException;\n\nimport com.google.android.vending.expansion.downloader.DownloaderClientMarshaller;\n\npublic class UnityAlarmReceiver extends BroadcastReceiver {\n\n    @Override\n    public void onReceive(Context context, Intent intent) {\n        try {\n            DownloaderClientMarshaller.startDownloadServiceIfRequired(context, intent, UnityDownloaderService.class);\n        } catch (NameNotFoundException e) {\n            e.printStackTrace();\n        }       \n    }\n\n}\n\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/unity3d/plugin/downloader/UnityDownloaderActivity.java",
    "content": "package com.unity3d.plugin.downloader;\n\nimport android.app.Activity;\nimport android.app.PendingIntent;\nimport android.graphics.Bitmap;\nimport android.graphics.BitmapFactory;\nimport android.os.Bundle;\nimport android.util.Log;\nimport android.content.Intent;\nimport android.os.Messenger;\nimport android.content.pm.PackageManager.NameNotFoundException;\nimport android.view.View;\nimport android.view.Window;\nimport android.widget.Button;\nimport android.widget.ImageView;\nimport android.widget.ProgressBar;\nimport android.widget.TextView;\nimport android.provider.Settings;\n\nimport java.io.InputStream;\n\nimport com.google.android.vending.expansion.downloader.*;\n\npublic class UnityDownloaderActivity extends Activity implements IDownloaderClient\n{\n    private ProgressBar mPB;\n\t\n    private TextView mStatusText;\n    private TextView mProgressFraction;\n    private TextView mProgressPercent;\n    private TextView mAverageSpeed;\n    private TextView mTimeRemaining;\n\t\n    private View mDashboard;\n    private View mCellMessage;\n\t\n    private Button mPauseButton;\n    private Button mWiFiSettingsButton;\n    \n    private boolean mStatePaused;\n    private int mState;\n\t\n    private IDownloaderService mRemoteService;\n\t\n    private IStub mDownloaderClientStub;\n    private static final String LOG_TAG = \"OBB\";\n\n    @Override\n    public void onCreate(Bundle savedInstanceState)\n    {\n        super.onCreate(savedInstanceState);\n\t\t\n\t\trequestWindowFeature(Window.FEATURE_NO_TITLE);\n\n\t\ttry {\n\t\t\tIntent launchIntent = getIntent();\n\t\t\tClass<?> mainActivity = Class.forName(launchIntent.getStringExtra(\"unityplayer.Activity\"));\n\t\t\tIntent intentToLaunchMainActivityFromNotification = new Intent(this, mainActivity);\n\t\t\tintentToLaunchMainActivityFromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);\n\t\t\tintentToLaunchMainActivityFromNotification.setAction(\"android.intent.action.MAIN\");\n\t\t\tintentToLaunchMainActivityFromNotification.addCategory(\"android.intent.category.LAUNCHER\");\n\t\t\t\n\t\t\t// Build PendingIntent used to open this activity from Notification\n\t\t\tPendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentToLaunchMainActivityFromNotification, PendingIntent.FLAG_UPDATE_CURRENT);\n\t\t\t// Request to start the download\n\t\t\tint startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(this, pendingIntent, UnityDownloaderService.class);\n\t\t\t\n\t\t\tif (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {\n\t\t\t\t// The DownloaderService has started downloading the files, show progress\n\t\t\t\tinitializeDownloadUI();\n\t\t\t\treturn;\n\t\t\t} // otherwise, download not needed so we fall through to starting the movie\n\t\t} catch (ClassNotFoundException e) {\n\t\t\tandroid.util.Log.e(LOG_TAG, \"Cannot find own package! MAYDAY!\");\n\t\t\te.printStackTrace();\n\t\t} catch (NameNotFoundException e) {\n\t\t\tandroid.util.Log.e(LOG_TAG, \"Cannot find own package! MAYDAY!\");\n\t\t\te.printStackTrace();\n\t\t}\n\t\tfinish();\n    }\n\n    /**\n     * Connect the stub to our service on resume.\n     */\n    @Override\n    protected void onResume() {\n        if (null != mDownloaderClientStub) {\n            mDownloaderClientStub.connect(this);\n        }\n        super.onResume();\n    }\n\t\n    /**\n     * Disconnect the stub from our service on stop\n     */\n    @Override\n    protected void onStop() {\n        if (null != mDownloaderClientStub) {\n            mDownloaderClientStub.disconnect(this);\n        }\n        super.onStop();\n    }\n\n    private void initializeDownloadUI() {\n        mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this, UnityDownloaderService.class);\n        setContentView(Helpers.getLayoutResource(this, \"main\"));\n\n\t\t// Set the background to the splash image generated by Unity\n       \ttry { \n\t\t\tInputStream is = getAssets().open(\"bin/Data/splash.png\");\n\t\t\tBitmap splashBitmap = null;\n\t\t\tBitmapFactory.Options options = new BitmapFactory.Options();\n\t\t\toptions.inPreferredConfig = Bitmap.Config.ARGB_8888;\n\t\t\tsplashBitmap = BitmapFactory.decodeStream(is, null, options);\n\t\t\tis.close();\n\t\t\tImageView splashImage = (ImageView) findViewById(Helpers.getIdResource(this, \"splashImage\"));\n\t\t\tsplashImage.setImageBitmap(splashBitmap);\n\t\t} catch(Exception e) { }\n\t\t\n        mPB = (ProgressBar) findViewById(Helpers.getIdResource(this, \"progressBar\"));\n        mStatusText = (TextView) findViewById(Helpers.getIdResource(this, \"statusText\"));\n        mProgressFraction = (TextView) findViewById(Helpers.getIdResource(this, \"progressAsFraction\"));\n        mProgressPercent = (TextView) findViewById(Helpers.getIdResource(this, \"progressAsPercentage\"));\n        mAverageSpeed = (TextView) findViewById(Helpers.getIdResource(this, \"progressAverageSpeed\"));\n        mTimeRemaining = (TextView) findViewById(Helpers.getIdResource(this, \"progressTimeRemaining\"));\n        mDashboard = findViewById(Helpers.getIdResource(this, \"downloaderDashboard\"));\n        mCellMessage = findViewById(Helpers.getIdResource(this, \"approveCellular\"));\n        mPauseButton = (Button) findViewById(Helpers.getIdResource(this, \"pauseButton\"));\n        mWiFiSettingsButton = (Button) findViewById(Helpers.getIdResource(this, \"wifiSettingsButton\"));\n\t\t\n        mPauseButton.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View view) {\n                if (mStatePaused) {\n                    mRemoteService.requestContinueDownload();\n                } else {\n                    mRemoteService.requestPauseDownload();\n                }\n                setButtonPausedState(!mStatePaused);\n            }\n        });\n        \n        mWiFiSettingsButton.setOnClickListener(new View.OnClickListener() {\n            \n            @Override\n            public void onClick(View v) {\n                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));                \n            }\n        });\n\t\t\n        Button resumeOnCell = (Button) findViewById(Helpers.getIdResource(this, \"resumeOverCellular\"));\n        resumeOnCell.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View view) {\n                mRemoteService.setDownloadFlags(IDownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR);\n                mRemoteService.requestContinueDownload();\n                mCellMessage.setVisibility(View.GONE);\n            }\n        });\n\t\t\n    }\n    private void setState(int newState) {\n        if (mState != newState) {\n            mState = newState;\n            mStatusText.setText(Helpers.getDownloaderStringResourceIDFromState(this, newState));\n        }\n    }\n\t\n    private void setButtonPausedState(boolean paused) {\n        mStatePaused = paused;\n        int stringResourceID = Helpers.getStringResource(this, paused ? \"text_button_resume\" : \"text_button_pause\");\n        mPauseButton.setText(stringResourceID);\n    }\n\t\n\t@Override\n\tpublic void onServiceConnected(Messenger m) {\n        mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);\n        mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());\n\t}\n\t@Override\n\tpublic void onDownloadStateChanged(int newState) {\n        setState(newState);\n        boolean showDashboard = true;\n        boolean showCellMessage = false;\n        boolean paused;\n        boolean indeterminate;\n        switch (newState) {\n            case IDownloaderClient.STATE_IDLE:\n                // STATE_IDLE means the service is listening, so it's\n                // safe to start making calls via mRemoteService.\n                paused = false;\n                indeterminate = true;\n                break;\n            case IDownloaderClient.STATE_CONNECTING:\n            case IDownloaderClient.STATE_FETCHING_URL:\n                showDashboard = true;\n                paused = false;\n                indeterminate = true;\n                break;\n            case IDownloaderClient.STATE_DOWNLOADING:\n                paused = false;\n                showDashboard = true;\n                indeterminate = false;\n                break;\n\t\t\t\t\n            case IDownloaderClient.STATE_FAILED_CANCELED:\n            case IDownloaderClient.STATE_FAILED:\n            case IDownloaderClient.STATE_FAILED_FETCHING_URL:\n            case IDownloaderClient.STATE_FAILED_UNLICENSED:\n                paused = true;\n                showDashboard = false;\n                indeterminate = false;\n                break;\n            case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION:\n            case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION:\n                showDashboard = false;\n                paused = true;\n                indeterminate = false;\n                showCellMessage = true;\n                break;\n            case IDownloaderClient.STATE_PAUSED_BY_REQUEST:\n                paused = true;\n                indeterminate = false;\n                break;\n            case IDownloaderClient.STATE_PAUSED_ROAMING:\n            case IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE:\n                paused = true;\n                indeterminate = false;\n                break;\n            case IDownloaderClient.STATE_COMPLETED:\n                showDashboard = false;\n                paused = false;\n                indeterminate = false;\n\t\t\t\tfinish();\n                return;\n            default:\n                paused = true;\n                indeterminate = true;\n                showDashboard = true;\n        }\n        int newDashboardVisibility = showDashboard ? View.VISIBLE : View.GONE;\n        if (mDashboard.getVisibility() != newDashboardVisibility) {\n            mDashboard.setVisibility(newDashboardVisibility);\n        }\n        int cellMessageVisibility = showCellMessage ? View.VISIBLE : View.GONE;\n        if (mCellMessage.getVisibility() != cellMessageVisibility) {\n            mCellMessage.setVisibility(cellMessageVisibility);\n        }\n        mPB.setIndeterminate(indeterminate);\n        setButtonPausedState(paused);\n\t}\n\t@Override\n\tpublic void onDownloadProgress(DownloadProgressInfo progress) {\n        mAverageSpeed.setText(getString(Helpers.getStringResource(this, \"kilobytes_per_second\"),\n\t\t\t\t\t\t\t\t\t\tHelpers.getSpeedString(progress.mCurrentSpeed)));\n        mTimeRemaining.setText(getString(Helpers.getStringResource(this, \"time_remaining\"),\n\t\t\t\t\t\t\t\t\t\t Helpers.getTimeRemaining(progress.mTimeRemaining)));\n\t\t\n        progress.mOverallTotal = progress.mOverallTotal;\n        mPB.setMax((int) (progress.mOverallTotal >> 8));\n        mPB.setProgress((int) (progress.mOverallProgress >> 8));\n        mProgressPercent.setText(Long.toString(progress.mOverallProgress\n\t\t\t\t\t\t\t\t\t\t\t   * 100 /\n\t\t\t\t\t\t\t\t\t\t\t   progress.mOverallTotal) + \"%\");\n        mProgressFraction.setText(Helpers.getDownloadProgressString\n\t\t\t\t\t\t\t\t  (progress.mOverallProgress,\n\t\t\t\t\t\t\t\t   progress.mOverallTotal));\n\t}\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/java/com/unity3d/plugin/downloader/UnityDownloaderService.java",
    "content": "package com.unity3d.plugin.downloader;\n\nimport com.google.android.vending.expansion.downloader.impl.DownloaderService;\n\npublic class UnityDownloaderService extends DownloaderService {\n    // stuff for LVL -- MODIFIED FROM C# SCRIPTS!\n    static String BASE64_PUBLIC_KEY = \"REPLACE THIS WITH YOUR PUBLIC KEY - DONE FROM C#\";\n    // used by the preference obfuscater\n    static byte[] SALT = new byte[] {\n            1, 43, -12, -1, 54, 98,\n            -100, -12, 43, 2, -8, -4, 9, 5, -106, -108, -33, 45, -1, 84\n    };\n\n    /**\n     * This public key comes from your Android Market publisher account, and it\n     * used by the LVL to validate responses from Market on your behalf.\n     */\n    @Override\n    public String getPublicKey() {\n        return BASE64_PUBLIC_KEY;\n    }\n\n    /**\n     * This is used by the preference obfuscater to make sure that your\n     * obfuscated preferences are different than the ones used by other\n     * applications.\n     */\n    @Override\n    public byte[] getSALT() {\n        return SALT;\n    }\n\n    /**\n     * Fill this in with the class name for your alarm receiver. We do this\n     * because receivers must be unique across all of Android (it's a good idea\n     * to make sure that your receiver is in your unique package)\n     */\n    @Override\n    public String getAlarmReceiverClassName() {\n        return UnityAlarmReceiver.class.getName();\n    }\n\n}\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/res/layout/main.xml",
    "content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<FrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\" >\n    <ImageView\n        android:id=\"@+id/splashImage\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\"/>\n    \n<LinearLayout\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\" >\n\n\n    <LinearLayout\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:layout_weight=\"0\"\n        android:orientation=\"vertical\" >\n\n        <TextView\n            android:id=\"@+id/statusText\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"wrap_content\"\n            android:layout_marginBottom=\"10dp\"\n            android:layout_marginLeft=\"5dp\"\n            android:layout_marginTop=\"10dp\"\n            android:textStyle=\"bold\" />\n\n        <LinearLayout\n            android:id=\"@+id/downloaderDashboard\"\n            android:layout_width=\"fill_parent\"\n            android:layout_height=\"wrap_content\"\n            android:layout_below=\"@id/statusText\"\n            android:orientation=\"vertical\" >\n\n            <RelativeLayout\n                android:layout_width=\"match_parent\"\n                android:layout_height=\"wrap_content\"\n                android:layout_weight=\"1\" >\n\n                <TextView\n                    android:id=\"@+id/progressAsFraction\"\n                    style=\"@android:style/TextAppearance.Small\"\n                    android:layout_width=\"wrap_content\"\n                    android:layout_height=\"wrap_content\"\n                    android:layout_alignParentLeft=\"true\"\n                    android:layout_marginLeft=\"5dp\"\n                    android:text=\"0MB / 0MB\" >\n                </TextView>\n\n                <TextView\n                    android:id=\"@+id/progressAsPercentage\"\n                    style=\"@android:style/TextAppearance.Small\"\n                    android:layout_width=\"wrap_content\"\n                    android:layout_height=\"wrap_content\"\n                    android:layout_alignRight=\"@+id/progressBar\"\n                    android:text=\"0%\" />\n\n                <ProgressBar\n                    android:id=\"@+id/progressBar\"\n                    style=\"?android:attr/progressBarStyleHorizontal\"\n                    android:layout_width=\"match_parent\"\n                    android:layout_height=\"wrap_content\"\n                    android:layout_below=\"@+id/progressAsFraction\"\n                    android:layout_marginBottom=\"10dp\"\n                    android:layout_marginLeft=\"10dp\"\n                    android:layout_marginRight=\"10dp\"\n                    android:layout_marginTop=\"10dp\"\n                    android:layout_weight=\"1\" />\n\n                <TextView\n                    android:id=\"@+id/progressAverageSpeed\"\n                    style=\"@android:style/TextAppearance.Small\"\n                    android:layout_width=\"wrap_content\"\n                    android:layout_height=\"wrap_content\"\n                    android:layout_alignParentLeft=\"true\"\n                    android:layout_below=\"@+id/progressBar\"\n                    android:layout_marginLeft=\"5dp\" />\n\n                <TextView\n                    android:id=\"@+id/progressTimeRemaining\"\n                    style=\"@android:style/TextAppearance.Small\"\n                    android:layout_width=\"wrap_content\"\n                    android:layout_height=\"wrap_content\"\n                    android:layout_alignRight=\"@+id/progressBar\"\n                    android:layout_below=\"@+id/progressBar\" />\n            </RelativeLayout>\n\n            <LinearLayout\n                android:id=\"@+id/downloaderDashboard\"\n                android:layout_width=\"fill_parent\"\n                android:layout_height=\"wrap_content\"\n                android:orientation=\"horizontal\" >\n\n                <Button\n                    android:id=\"@+id/pauseButton\"\n                    android:layout_width=\"wrap_content\"\n                    android:layout_height=\"wrap_content\"\n                    android:layout_gravity=\"center_vertical\"\n                    android:layout_marginBottom=\"10dp\"\n                    android:layout_marginLeft=\"10dp\"\n                    android:layout_marginRight=\"5dp\"\n                    android:layout_marginTop=\"10dp\"\n                    android:layout_weight=\"0\"\n                    android:minHeight=\"40dp\"\n                    android:minWidth=\"94dp\"\n                    android:text=\"@string/text_button_pause\" />\n\n                <Button\n                    android:id=\"@+id/cancelButton\"\n                    android:layout_width=\"wrap_content\"\n                    android:layout_height=\"wrap_content\"\n                    android:layout_gravity=\"center_vertical\"\n                    android:layout_marginBottom=\"10dp\"\n                    android:layout_marginLeft=\"5dp\"\n                    android:layout_marginRight=\"5dp\"\n                    android:layout_marginTop=\"10dp\"\n                    android:layout_weight=\"0\"\n                    android:minHeight=\"40dp\"\n                    android:minWidth=\"94dp\"\n                    android:text=\"@string/text_button_cancel\"\n                    android:visibility=\"gone\" />\n            </LinearLayout>\n        </LinearLayout>\n    </LinearLayout>\n\n    <LinearLayout\n        android:id=\"@+id/approveCellular\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\"\n        android:layout_weight=\"1\"\n        android:orientation=\"vertical\"\n        android:visibility=\"gone\" >\n\n        <TextView\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"wrap_content\"\n            android:layout_margin=\"10dp\"\n            android:id=\"@+id/textPausedParagraph1\"\n            android:text=\"@string/text_paused_cellular\" />\n\n        <TextView\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"wrap_content\"\n            android:layout_margin=\"10dp\"\n            android:id=\"@+id/textPausedParagraph2\"\n            android:text=\"@string/text_paused_cellular_2\" />\n\n        <LinearLayout\n            android:id=\"@+id/buttonRow\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"wrap_content\"\n            android:orientation=\"horizontal\" >\n\n            <Button\n                android:id=\"@+id/resumeOverCellular\"\n                android:layout_width=\"wrap_content\"\n                android:layout_height=\"wrap_content\"\n                android:layout_gravity=\"center\"\n                android:layout_margin=\"10dp\"\n                android:text=\"@string/text_button_resume_cellular\" />\n\n            <Button\n                android:id=\"@+id/wifiSettingsButton\"\n                android:layout_width=\"wrap_content\"\n                android:layout_height=\"wrap_content\"\n                android:layout_gravity=\"center\"\n                android:layout_margin=\"10dp\"\n                android:text=\"@string/text_button_wifi_settings\" />\n            </LinearLayout>\n    </LinearLayout>\n\n</LinearLayout>\n</FrameLayout>\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/res/layout/status_bar_ongoing_event_progress_bar.xml",
    "content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!--\n/*\n** Copyright 2012, The Android Open Source Project\n**\n** Licensed under the Apache License, Version 2.0 (the \"License\");\n** you may not use this file except in compliance with the License.\n** You may obtain a copy of the License at\n**\n**     http://www.apache.org/licenses/LICENSE-2.0\n**\n** Unless required by applicable law or agreed to in writing, software\n** distributed under the License is distributed on an \"AS IS\" BASIS,\n** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n** See the License for the specific language governing permissions and\n** limitations under the License.\n*/\n-->\n\n<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"horizontal\" \n    android:id=\"@+id/notificationLayout\"\n    android:baselineAligned=\"false\">\n\n    <RelativeLayout\n        android:layout_width=\"@android:dimen/notification_large_icon_width\"\n        android:layout_height=\"match_parent\"\n\t >\n\n        <ImageView\n            android:id=\"@+id/appIcon\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"match_parent\"\n            android:scaleType=\"centerInside\"            \n            android:layout_alignParentLeft=\"true\"\n            android:layout_alignParentTop=\"true\"            \n\t\t\tandroid:background=\"@drawable/notify_panel_notification_icon_bg\"\n            android:src=\"@android:drawable/stat_sys_download\" />\n\n    </RelativeLayout>\n\n    <RelativeLayout\n        android:layout_width=\"0dp\"\n        android:layout_height=\"match_parent\"\n        android:layout_weight=\"1.0\"\n        android:paddingLeft=\"16dp\"\n        android:paddingTop=\"10dp\"\n        android:paddingRight=\"8dp\"\n        android:paddingBottom=\"8dp\" >\n\n        <TextView\n            android:id=\"@+id/title\"\n            style=\"@style/NotificationTitle\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:layout_alignParentLeft=\"true\"\n            android:singleLine=\"true\"/>\n\n        <TextView\n            android:id=\"@+id/time_remaining\"\n            style=\"@style/NotificationText\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:layout_alignParentRight=\"true\"\n            android:singleLine=\"true\"/>\n\n        <TextView\n            android:id=\"@+id/description\"\n            style=\"@style/NotificationTextSecondary\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:paddingRight=\"25dp\"\n            android:singleLine=\"true\"\n            android:layout_below=\"@id/title\" />\n        \n        <ProgressBar\n            android:id=\"@+id/progress_bar\"\n            style=\"?android:attr/progressBarStyleHorizontal\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"wrap_content\"\n            android:paddingRight=\"25dp\"\n            android:layout_below=\"@id/description\" />\n        \n    </RelativeLayout>\n\n</LinearLayout>"
  },
  {
    "path": "src/unityOBBDownloader/src/main/res/values/main-strings.xml",
    "content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n<!--\n    <string name=\"app_name\">APKX Downloader</string> -->\n    <string name=\"text_paused_cellular\">Would you like to enable downloading over cellular connections? Depending on your data plan, this may cost you money.</string>\n    <string name=\"text_paused_cellular_2\">If you choose not to enable downloading over cellular connections, the download will automatically resume when wi-fi is available.</string>\n    <string name=\"text_button_resume_cellular\">Resume download</string>\n    <string name=\"text_button_wifi_settings\">Wi-Fi settings</string>\n    <string name=\"text_verifying_download\">Verifying Download</string>\n    <string name=\"text_validation_complete\">XAPK File Validation Complete.  Select OK to start the movie.</string>\n    <string name=\"text_validation_failed\">XAPK File Validation Failed.</string>\n    <string name=\"text_button_pause\">Pause Download</string>\n    <string name=\"text_button_resume\">Resume Download</string>\n    <string name=\"text_button_cancel\">Cancel</string>\n    <string name=\"text_button_cancel_verify\">Cancel Verification</string>\n\n</resources>\n"
  },
  {
    "path": "src/unityOBBDownloader/src/main/res/values/strings.xml",
    "content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n\n    <!-- When a download completes, a notification is displayed, and this\n        string is used to indicate that the download successfully completed.\n        Note that such a download could have been initiated by a variety of\n        applications, including (but not limited to) the browser, an email\n        application, a content marketplace. -->\n    <string name=\"notification_download_complete\">Download complete</string>\n\n    <!-- When a download completes, a notification is displayed, and this\n        string is used to indicate that the download failed.\n        Note that such a download could have been initiated by a variety of\n        applications, including (but not limited to) the browser, an email\n        application, a content marketplace. -->\n    <string name=\"notification_download_failed\">Download unsuccessful</string>\n\n\n    <string name=\"state_unknown\">Starting...\"</string>\n    <string name=\"state_idle\">Waiting for download to start</string>\n    <string name=\"state_fetching_url\">Looking for resources to download</string>\n    <string name=\"state_connecting\">Connecting to the download server</string>\n    <string name=\"state_downloading\">Downloading resources</string>\n    <string name=\"state_completed\">Download finished</string>\n    <string name=\"state_paused_network_unavailable\">Download paused because no network is available</string>\n    <string name=\"state_paused_network_setup_failure\">Download paused. Test a website in browser</string>\n    <string name=\"state_paused_by_request\">Download paused</string>\n    <string name=\"state_paused_wifi_unavailable\">Download paused because wifi is unavailable</string>\n    <string name=\"state_paused_wifi_disabled\">Download paused because wifi is disabled</string>\n    <string name=\"state_paused_roaming\">Download paused because you are roaming</string>\n    <string name=\"state_paused_sdcard_unavailable\">Download paused because the external storage is unavailable</string>\n    <string name=\"state_failed_unlicensed\">Download failed because you may not have purchased this app</string>\n    <string name=\"state_failed_fetching_url\">Download failed because the resources could not be found</string>\n    <string name=\"state_failed_sdcard_full\">Download failed because the external storage is full</string>\n    <string name=\"state_failed_cancelled\">Download cancelled</string>\n    <string name=\"state_failed\">Download failed</string>\n\n    <string name=\"kilobytes_per_second\">%1$s KB/s</string>\n    <string name=\"time_remaining\">Time remaining: %1$s</string>\n    <string name=\"time_remaining_notification\">%1$s left</string>\n</resources>"
  },
  {
    "path": "src/unityOBBDownloader/src/main/res/values/styles.xml",
    "content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n\n    <style name=\"NotificationText\">\n        <item name=\"android:textColor\">?android:attr/textColorPrimary</item>\n    </style>\n\n    <style name=\"NotificationTextShadow\" parent=\"NotificationText\">\n        <item name=\"android:textColor\">?android:attr/textColorPrimary</item>\n        <item name=\"android:shadowColor\">@android:color/background_dark</item>\n        <item name=\"android:shadowDx\">1.0</item>\n        <item name=\"android:shadowDy\">1.0</item>\n        <item name=\"android:shadowRadius\">1</item>\n    </style>\n\n    <style name=\"NotificationTitle\">\n        <item name=\"android:textColor\">?android:attr/textColorPrimary</item>\n        <item name=\"android:textStyle\">bold</item>\n    </style>\n\n    <style name=\"ButtonBackground\">\n        <item name=\"android:background\">@android:color/background_dark</item>\n    </style>\n\n    <style name=\"NotificationTextSecondary\" parent=\"NotificationText\">\n        <item name=\"android:textSize\">12sp</item>\n    </style>\n\n</resources>"
  }
]