|
1 | 1 | package ly.count.android.demo; |
2 | 2 |
|
3 | 3 | import android.os.Bundle; |
| 4 | +import android.view.LayoutInflater; |
4 | 5 | import android.view.View; |
| 6 | +import android.widget.EditText; |
| 7 | +import android.widget.LinearLayout; |
| 8 | +import android.widget.Toast; |
| 9 | + |
5 | 10 | import androidx.appcompat.app.AppCompatActivity; |
| 11 | + |
| 12 | +import com.google.android.material.button.MaterialButton; |
| 13 | +import com.google.android.material.textfield.TextInputEditText; |
| 14 | + |
| 15 | +import java.util.HashMap; |
6 | 16 | import java.util.Map; |
7 | | -import java.util.Random; |
8 | | -import java.util.concurrent.ConcurrentHashMap; |
| 17 | + |
9 | 18 | import ly.count.android.sdk.Countly; |
10 | 19 |
|
11 | | -@SuppressWarnings("UnusedParameters") |
12 | 20 | public class ActivityExampleCustomEvents extends AppCompatActivity { |
13 | 21 |
|
| 22 | + private TextInputEditText inputEventName, inputEventCount, inputEventSum, inputEventDuration; |
| 23 | + private TextInputEditText inputTimedEventName; |
| 24 | + private LinearLayout segmentationContainer; |
| 25 | + |
14 | 26 | @Override |
15 | 27 | public void onCreate(Bundle savedInstanceState) { |
16 | 28 | super.onCreate(savedInstanceState); |
17 | 29 | setContentView(R.layout.activity_example_custom_events); |
18 | | - } |
19 | 30 |
|
20 | | - public void onClickRecordEvent01(View v) { |
21 | | - Countly.sharedInstance().events().recordEvent("Custom event 1"); |
| 31 | + inputEventName = findViewById(R.id.inputEventName); |
| 32 | + inputEventCount = findViewById(R.id.inputEventCount); |
| 33 | + inputEventSum = findViewById(R.id.inputEventSum); |
| 34 | + inputEventDuration = findViewById(R.id.inputEventDuration); |
| 35 | + inputTimedEventName = findViewById(R.id.inputTimedEventName); |
| 36 | + segmentationContainer = findViewById(R.id.segmentationContainer); |
| 37 | + |
| 38 | + findViewById(R.id.btnAddSegment).setOnClickListener(v -> addSegmentRow()); |
| 39 | + findViewById(R.id.btnRecordEvent).setOnClickListener(v -> recordCustomEvent()); |
| 40 | + findViewById(R.id.btnStartTimedEvent).setOnClickListener(v -> startTimedEvent()); |
| 41 | + findViewById(R.id.btnEndTimedEvent).setOnClickListener(v -> endTimedEvent()); |
| 42 | + findViewById(R.id.btnCancelTimedEvent).setOnClickListener(v -> cancelTimedEvent()); |
| 43 | + findViewById(R.id.btnPreset1).setOnClickListener(v -> recordPresetWithSegmentation()); |
| 44 | + findViewById(R.id.btnPreset2).setOnClickListener(v -> recordPresetWithCountSum()); |
| 45 | + findViewById(R.id.btnTriggerSend).setOnClickListener(v -> { |
| 46 | + Countly.sharedInstance().requestQueue().attemptToSendStoredRequests(); |
| 47 | + Toast.makeText(this, "Sending stored requests", Toast.LENGTH_SHORT).show(); |
| 48 | + }); |
| 49 | + |
| 50 | + // Add one default segment row |
| 51 | + addSegmentRow(); |
22 | 52 | } |
23 | 53 |
|
24 | | - public void onClickRecordEvent02(View v) { |
25 | | - Countly.sharedInstance().events().recordEvent("Custom event 2", 3); |
| 54 | + private void addSegmentRow() { |
| 55 | + View row = LayoutInflater.from(this).inflate(R.layout.row_segmentation, segmentationContainer, false); |
| 56 | + row.findViewById(R.id.btnRemoveSegment).setOnClickListener(v -> segmentationContainer.removeView(row)); |
| 57 | + segmentationContainer.addView(row); |
26 | 58 | } |
27 | 59 |
|
28 | | - public void onClickRecordEvent03(View v) { |
29 | | - Countly.sharedInstance().events().recordEvent("Custom event 3", 1, 134); |
| 60 | + private Map<String, Object> collectSegmentation() { |
| 61 | + Map<String, Object> segmentation = new HashMap<>(); |
| 62 | + for (int i = 0; i < segmentationContainer.getChildCount(); i++) { |
| 63 | + View row = segmentationContainer.getChildAt(i); |
| 64 | + EditText keyField = row.findViewById(R.id.inputSegKey); |
| 65 | + EditText valueField = row.findViewById(R.id.inputSegValue); |
| 66 | + String key = keyField.getText().toString().trim(); |
| 67 | + String value = valueField.getText().toString().trim(); |
| 68 | + if (!key.isEmpty() && !value.isEmpty()) { |
| 69 | + // Try to parse as number |
| 70 | + try { |
| 71 | + if (value.contains(".")) { |
| 72 | + segmentation.put(key, Double.parseDouble(value)); |
| 73 | + } else { |
| 74 | + segmentation.put(key, Integer.parseInt(value)); |
| 75 | + } |
| 76 | + } catch (NumberFormatException e) { |
| 77 | + segmentation.put(key, value); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return segmentation; |
30 | 82 | } |
31 | 83 |
|
32 | | - public void onClickRecordEvent04(View v) { |
33 | | - Countly.sharedInstance().events().recordEvent("Custom event 4", null, 1, 0, 55); |
| 84 | + private void recordCustomEvent() { |
| 85 | + String eventName = inputEventName.getText() != null ? inputEventName.getText().toString().trim() : ""; |
| 86 | + if (eventName.isEmpty()) { |
| 87 | + inputEventName.setError("Event name is required"); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + int count = 1; |
| 92 | + double sum = 0; |
| 93 | + double duration = 0; |
| 94 | + |
| 95 | + try { |
| 96 | + String countStr = inputEventCount.getText() != null ? inputEventCount.getText().toString().trim() : ""; |
| 97 | + if (!countStr.isEmpty()) count = Integer.parseInt(countStr); |
| 98 | + } catch (NumberFormatException ignored) {} |
| 99 | + |
| 100 | + try { |
| 101 | + String sumStr = inputEventSum.getText() != null ? inputEventSum.getText().toString().trim() : ""; |
| 102 | + if (!sumStr.isEmpty()) sum = Double.parseDouble(sumStr); |
| 103 | + } catch (NumberFormatException ignored) {} |
| 104 | + |
| 105 | + try { |
| 106 | + String durStr = inputEventDuration.getText() != null ? inputEventDuration.getText().toString().trim() : ""; |
| 107 | + if (!durStr.isEmpty()) duration = Double.parseDouble(durStr); |
| 108 | + } catch (NumberFormatException ignored) {} |
| 109 | + |
| 110 | + Map<String, Object> segmentation = collectSegmentation(); |
| 111 | + |
| 112 | + if (segmentation.isEmpty()) { |
| 113 | + Countly.sharedInstance().events().recordEvent(eventName, count, sum); |
| 114 | + } else { |
| 115 | + Countly.sharedInstance().events().recordEvent(eventName, segmentation, count, sum, duration); |
| 116 | + } |
| 117 | + |
| 118 | + Toast.makeText(this, "Event '" + eventName + "' recorded", Toast.LENGTH_SHORT).show(); |
34 | 119 | } |
35 | 120 |
|
36 | | - public void onClickRecordEvent05(View v) { |
37 | | - Map<String, Object> segmentation = new ConcurrentHashMap<>(); |
38 | | - segmentation.put("wall", "green"); |
39 | | - Countly.sharedInstance().events().recordEvent("Custom event 5", segmentation, 1, 0, 0); |
| 121 | + private void startTimedEvent() { |
| 122 | + String name = inputTimedEventName.getText() != null ? inputTimedEventName.getText().toString().trim() : ""; |
| 123 | + if (name.isEmpty()) { |
| 124 | + inputTimedEventName.setError("Event name is required"); |
| 125 | + return; |
| 126 | + } |
| 127 | + boolean started = Countly.sharedInstance().events().startEvent(name); |
| 128 | + Toast.makeText(this, started ? "Timed event '" + name + "' started" : "Could not start (already running?)", Toast.LENGTH_SHORT).show(); |
40 | 129 | } |
41 | 130 |
|
42 | | - public void onClickRecordEvent06(View v) { |
43 | | - Map<String, Object> segmentation = new ConcurrentHashMap<>(); |
44 | | - segmentation.put("wall", "red"); |
45 | | - segmentation.put("flowers", 3); |
46 | | - segmentation.put("area", 1.23); |
47 | | - segmentation.put("volume", 7.88); |
48 | | - Countly.sharedInstance().events().recordEvent("Custom event 6", segmentation, 15, 0, 0); |
| 131 | + private void endTimedEvent() { |
| 132 | + String name = inputTimedEventName.getText() != null ? inputTimedEventName.getText().toString().trim() : ""; |
| 133 | + if (name.isEmpty()) { |
| 134 | + inputTimedEventName.setError("Event name is required"); |
| 135 | + return; |
| 136 | + } |
| 137 | + boolean ended = Countly.sharedInstance().events().endEvent(name); |
| 138 | + Toast.makeText(this, ended ? "Timed event '" + name + "' ended" : "Could not end (not running?)", Toast.LENGTH_SHORT).show(); |
49 | 139 | } |
50 | 140 |
|
51 | | - public void onClickRecordEvent07(View v) { |
52 | | - Map<String, Object> segmentation = new ConcurrentHashMap<>(); |
53 | | - segmentation.put("wall", "blue"); |
54 | | - segmentation.put("flowers", new Random().nextInt()); |
55 | | - segmentation.put("area", new Random().nextDouble()); |
56 | | - segmentation.put("volume", new Random().nextDouble()); |
57 | | - |
58 | | - Countly.sharedInstance().events().recordEvent("Custom event 7", segmentation, 25, 10, 0); |
| 141 | + private void cancelTimedEvent() { |
| 142 | + String name = inputTimedEventName.getText() != null ? inputTimedEventName.getText().toString().trim() : ""; |
| 143 | + if (name.isEmpty()) { |
| 144 | + inputTimedEventName.setError("Event name is required"); |
| 145 | + return; |
| 146 | + } |
| 147 | + boolean cancelled = Countly.sharedInstance().events().cancelEvent(name); |
| 148 | + Toast.makeText(this, cancelled ? "Timed event '" + name + "' cancelled" : "Could not cancel (not running?)", Toast.LENGTH_SHORT).show(); |
59 | 149 | } |
60 | 150 |
|
61 | | - public void onClickRecordEvent08(View v) { |
62 | | - Map<String, Object> segmentation = new ConcurrentHashMap<>(); |
63 | | - segmentation.put("wall", "yellow"); |
64 | | - Countly.sharedInstance().events().recordEvent("Custom event 8", segmentation, 25, 10, 50); |
65 | | - } |
66 | | - |
67 | | - public void onClickRecordEvent09(View v) { |
68 | | - //start timed event |
69 | | - Countly.sharedInstance().events().recordEvent("Custom event 9"); |
70 | | - } |
71 | | - |
72 | | - public void onClickRecordEvent10(View v) { |
73 | | - //stop timed event |
74 | | - Countly.sharedInstance().events().recordEvent("Custom event 9"); |
75 | | - } |
76 | | - |
77 | | - public void onClickRecordEvent12(View v) { |
78 | | - //cancel timed event |
79 | | - Countly.sharedInstance().events().cancelEvent("Custom event 9"); |
80 | | - } |
81 | | - |
82 | | - public void onClickRecordEvent11(View v) { |
83 | | - Map<String, Object> segmentation = new ConcurrentHashMap<>(); |
84 | | - segmentation.put("wall", "orange"); |
85 | | - Countly.sharedInstance().events().recordEvent("Custom event 9", segmentation, 4, 34); |
| 151 | + private void recordPresetWithSegmentation() { |
| 152 | + Map<String, Object> segmentation = new HashMap<>(); |
| 153 | + segmentation.put("wall", "green"); |
| 154 | + segmentation.put("flowers", 3); |
| 155 | + Countly.sharedInstance().events().recordEvent("Preset Segmentation Event", segmentation, 1, 0, 0); |
| 156 | + Toast.makeText(this, "Preset segmentation event recorded", Toast.LENGTH_SHORT).show(); |
86 | 157 | } |
87 | 158 |
|
88 | | - public void onClickTriggerSendingEvents(View v) { |
89 | | - Countly.sharedInstance().requestQueue().attemptToSendStoredRequests(); |
| 159 | + private void recordPresetWithCountSum() { |
| 160 | + Map<String, Object> segmentation = new HashMap<>(); |
| 161 | + segmentation.put("wall", "blue"); |
| 162 | + Countly.sharedInstance().events().recordEvent("Preset Count Sum Event", segmentation, 5, 24.5, 0); |
| 163 | + Toast.makeText(this, "Preset count+sum event recorded", Toast.LENGTH_SHORT).show(); |
90 | 164 | } |
91 | 165 | } |
0 commit comments