Skip to content

Instantly share code, notes, and snippets.

@code-crusher
Created September 11, 2019 00:31
Show Gist options
  • Save code-crusher/1a85b36261be906321ab5494d65e4269 to your computer and use it in GitHub Desktop.
Save code-crusher/1a85b36261be906321ab5494d65e4269 to your computer and use it in GitHub Desktop.
tune-android-sdk-systemproperties

tune-android-sdk-systemproperties

Steps to integrate:

Copy the below code in your app into a new file TunePreloadSystemData.java:

class TunePreloadSystemData {

    private static final String SYSTEM_PROPERTIES_CLASS_KEY = "android.os.SystemProperties";
    private static final String TUNE_BRANCH_PREINSTALL_PROP_KEY = "io.branch.tune.preinstall.apps.path";

   public static void getPreinstallSystemData(TuneInternal sTuneInstance) {
        if (sTuneInstance != null) {
            // check if the SystemProperties has the tune<>branch file path added
            String tuneFilePath = checkForTunePreloadInSystem();
            if (!TextUtils.isEmpty(tuneFilePath)) {
                // after getting the file path get the file contents
                readTuneFile(tuneFilePath, sTuneInstance);
            }
        }
    }

    private static String checkForTunePreloadInSystem() {
        String path = null;
        try {
            path = (String) Class.forName(SYSTEM_PROPERTIES_CLASS_KEY)
                    .getMethod("get", String.class).invoke(null, TUNE_BRANCH_PREINSTALL_PROP_KEY);
        } catch (Exception e) {
            return null;
        }
        return path;
    }

    static void readTuneFile(final String tuneFilePath, final TuneInternal sTuneInstance) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final StringBuilder tuneFileContent = new StringBuilder();
                    JSONObject tuneFileContentJson;
                    File tuneFile = new File(tuneFilePath);
                    BufferedReader br = new BufferedReader(new FileReader(tuneFile));
                    String line;

                    while ((line = br.readLine()) != null) {
                        tuneFileContent.append(line);
                    }
                    br.close();
                    tuneFileContentJson = new JSONObject(tuneFileContent.toString().trim());

                    if (!TextUtils.isEmpty(tuneFileContentJson.toString())) {
                        getTuneFileContent(tuneFileContentJson, sTuneInstance);
                    } else {
                        throw new FileNotFoundException();
                    }
                } catch (FileNotFoundException ignore) {
                } catch (IOException ignore) {
                } catch (JSONException ignore) {
                }
            }
        }).start();
    }

    public static void getTuneFileContent(JSONObject tuneFileContentJson,
            TuneInternal sTuneInstance) {
        // check if the current app package exists in the json
        Iterator<String> keys = tuneFileContentJson.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            try {
                if (key.equals("apps") && tuneFileContentJson
                        .get(key) instanceof JSONObject) {
                    if (tuneFileContentJson.getJSONObject(key)
                            .get(sTuneInstance.getPackageName()) != null) {
                        JSONObject tunePreinstallData = tuneFileContentJson
                                .getJSONObject(key)
                                .getJSONObject(sTuneInstance.getPackageName());

                        // find the preinstalls keys and any custom data
                        Iterator<String> preinstallDataKeys = tunePreinstallData
                                .keys();

                        TunePreloadData updatedTunePreloadData = null;

                        if (!TextUtils
                                .isEmpty(tunePreinstallData.getString(TuneUrlKeys.PUBLISHER_ID))) {
                            updatedTunePreloadData = new TunePreloadData(
                                    tunePreinstallData.getString(
                                            TuneUrlKeys.PUBLISHER_ID));

                            while (preinstallDataKeys.hasNext()) {
                                String datakey = preinstallDataKeys.next();
                                if (datakey.equals(TuneUrlKeys.OFFER_ID) && !TextUtils
                                        .isEmpty(tunePreinstallData.get(datakey)
                                                .toString())) {
                                    updatedTunePreloadData
                                            .withOfferId(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.AGENCY_ID) && !TextUtils
                                        .isEmpty(tunePreinstallData.get(datakey)
                                                .toString())) {
                                    updatedTunePreloadData
                                            .withAgencyId(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_REF_ID)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData.withPublisherReferenceId(
                                            tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_SUB1) && !TextUtils
                                        .isEmpty(tunePreinstallData.get(datakey)
                                                .toString())) {
                                    updatedTunePreloadData
                                            .withPublisherSub1(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_SUB2) && !TextUtils
                                        .isEmpty(tunePreinstallData.get(datakey)
                                                .toString())) {
                                    updatedTunePreloadData
                                            .withPublisherSub2(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_SUB3) && !TextUtils
                                        .isEmpty(tunePreinstallData.get(datakey)
                                                .toString())) {
                                    updatedTunePreloadData
                                            .withPublisherSub3(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_SUB4) && !TextUtils
                                        .isEmpty(tunePreinstallData.get(datakey)
                                                .toString())) {
                                    updatedTunePreloadData
                                            .withPublisherSub4(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_SUB5) && !TextUtils
                                        .isEmpty(tunePreinstallData.get(datakey)
                                                .toString())) {
                                    updatedTunePreloadData
                                            .withPublisherSub5(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_SUB_AD)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData
                                            .withAdvertiserSubAd(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_SUB_ADGROUP)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData
                                            .withPublisherSubAdgroup(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_SUB_CAMPAIGN)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData.withPublisherSubCampaign(
                                            tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_SUB_KEYWORD)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData
                                            .withPublisherSubKeyword(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.PUBLISHER_SUB_PUBLISHER)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData.withPublisherSubPublisher(
                                            tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.ADVERTISER_SUB_AD)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData
                                            .withAdvertiserSubAd(tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.ADVERTISER_SUB_ADGROUP)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData.withAdvertiserSubAdgroup(
                                            tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.ADVERTISER_SUB_CAMPAIGN)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData.withAdvertiserSubCampaign(
                                            tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.ADVERTISER_SUB_CAMPAIGN)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData.withAdvertiserSubCampaign(
                                            tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.ADVERTISER_SUB_KEYWORD)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData.withAdvertiserSubKeyword(
                                            tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.ADVERTISER_SUB_PUBLISHER)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData.withAdvertiserSubPublisher(
                                            tunePreinstallData.get(datakey)
                                                    .toString());
                                } else if (datakey.equals(TuneUrlKeys.ADVERTISER_SUB_SITE)
                                        && !TextUtils.isEmpty(tunePreinstallData.get(datakey)
                                        .toString())) {
                                    updatedTunePreloadData
                                            .withAdvertiserSubSite(tunePreinstallData.get(datakey)
                                                    .toString());
                                }
                            }

                            // set the final TunePreloadData
                            sTuneInstance.setPreloadedAppData(updatedTunePreloadData);
                        }
                    }
                }
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }
}

Once added, then call the below method in place of the #setPreloadedAppData():

  TunePreloadSystemData.getPreinstallSystemData(TuneInternal.getInstance());

OEM side setup:

The OEM needs to add the following file pre_install_apps.tune:

{
    "apps": {
        "com.tune.test": {
            "publisher_id": "test_publisher_id",
            "publisher_sub_campaign": "test_publisher_sub_campaign"
        }
    }
}
  • The supported keys are:
// Preloaded app keys
"publisher_id";
"offer_id";
"agency_id";
"publisher_ref_id";
"publisher_sub_publisher";
"publisher_sub_site";
"publisher_sub_campaign";
"publisher_sub_adgroup";
"publisher_sub_ad";
"publisher_sub_keyword";
"publisher_sub1";
"publisher_sub2";
"publisher_sub3";
"publisher_sub4";
"publisher_sub5";
"advertiser_sub_publisher";
"advertiser_sub_site";
"advertiser_sub_campaign";
"advertiser_sub_adgroup";
"advertiser_sub_ad";
"advertiser_sub_keyword";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment