Module: BranchIOCLI::Helper::AndroidHelper

Included in:
BranchHelper
Defined in:
lib/branch_io_cli/helper/android_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_intent_filter_to_activity(activity, domains, uri_scheme) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/branch_io_cli/helper/android_helper.rb', line 48

def add_intent_filter_to_activity(activity, domains, uri_scheme)
  # Add a single intent-filter with autoVerify and a data element for each domain and the optional uri_scheme
  intent_filter = REXML::Element.new "intent-filter"
  intent_filter.attributes["android:autoVerify"] = true
  intent_filter.add_element "action", "android:name" => "android.intent.action.VIEW"
  intent_filter.add_element "category", "android:name" => "android.intent.category.DEFAULT"
  intent_filter.add_element "category", "android:name" => "android.intent.category.BROWSABLE"
  intent_filter.elements << uri_scheme_data_element(uri_scheme) unless uri_scheme.nil?
  app_link_data_elements(domains).each { |e| intent_filter.elements << e }

  activity.add_element intent_filter
end

#add_intent_filters_to_android_manifest(manifest, domains, uri_scheme, activity_name, remove_existing) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/branch_io_cli/helper/android_helper.rb', line 21

def add_intent_filters_to_android_manifest(manifest, domains, uri_scheme, activity_name, remove_existing)
  if activity_name
    activity = manifest.elements["//manifest/application/activity[@android:name=\"#{activity_name}\""]
  else
    activity = find_activity manifest
  end

  raise "Failed to find an Activity in the Android manifest" if activity.nil?

  if remove_existing
    remove_existing_domains(activity)
  end

  add_intent_filter_to_activity activity, domains, uri_scheme
end

#add_keys_to_android_manifest(manifest, keys) ⇒ Object



4
5
6
7
# File 'lib/branch_io_cli/helper/android_helper.rb', line 4

def add_keys_to_android_manifest(manifest, keys)
   manifest, "io.branch.sdk.BranchKey", keys[:live] unless keys[:live].nil?
   manifest, "io.branch.sdk.BranchKey.test", keys[:test] unless keys[:test].nil?
end

#add_metadata_to_manifest(manifest, key, value) ⇒ Object

TODO: Work on all XML/AndroidManifest formatting



11
12
13
14
15
16
17
18
19
# File 'lib/branch_io_cli/helper/android_helper.rb', line 11

def (manifest, key, value)
  element = manifest.elements["//manifest/application/meta-data[@android:name=\"#{key}\"]"]
  if element.nil?
    application = manifest.elements["//manifest/application"]
    application.add_element "meta-data", "android:name" => key, "android:value" => value
  else
    element.attributes["android:value"] = value
  end
end


69
70
71
72
73
74
75
76
# File 'lib/branch_io_cli/helper/android_helper.rb', line 69

def app_link_data_elements(domains)
  domains.map do |domain|
    element = REXML::Element.new "data"
    element.attributes["android:scheme"] = "https"
    element.attributes["android:host"] = domain
    element
  end
end

#find_activity(manifest) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/branch_io_cli/helper/android_helper.rb', line 37

def find_activity(manifest)
  # try to infer the right activity
  # look for the first singleTask
  single_task_activity = manifest.elements["//manifest/application/activity[@android:launchMode=\"singleTask\"]"]
  return single_task_activity if single_task_activity

  # no singleTask activities. Take the first Activity
  # TODO: Add singleTask?
  manifest.elements["//manifest/application/activity"]
end

#remove_existing_domains(activity) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/branch_io_cli/helper/android_helper.rb', line 61

def remove_existing_domains(activity)
  # Find all intent-filters that include a data element with android:scheme
  # TODO: Can this be done with a single css/at_css call?
  activity.elements.each("//manifest//intent-filter") do |filter|
    filter.remove if filter.elements["data[@android:scheme]"]
  end
end

#uri_scheme_data_element(uri_scheme) ⇒ Object



78
79
80
81
82
83
# File 'lib/branch_io_cli/helper/android_helper.rb', line 78

def uri_scheme_data_element(uri_scheme)
  element = REXML::Element.new "data"
  element.attributes["android:scheme"] = uri_scheme
  element.attributes["android:host"] = "open"
  element
end