Skip to content

Commit 0b31e67

Browse files
committed
Updated Alarms, Browser, Calculator, Calendar and Contacts.
Updated sample.
1 parent 16a2760 commit 0b31e67

File tree

14 files changed

+383
-356
lines changed

14 files changed

+383
-356
lines changed
Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,20 @@
11
package com.next.androidintentlibrary;
22

33
import android.app.Activity;
4-
import android.content.ActivityNotFoundException;
54
import android.content.Context;
65
import android.content.Intent;
76
import android.os.Build;
87
import android.provider.AlarmClock;
8+
99
import androidx.annotation.NonNull;
1010
import androidx.annotation.RequiresApi;
1111

1212
public class AlarmIntents
1313
{
14-
//TODO: add to readme
15-
// Resources
16-
// https://Stackoverflow.com android intent documentation (no longer available)
17-
// https://github.com/d-tarasov/android-intents
18-
// https://developer.android.com/guide/components/intents-common.html
19-
// https://github.com/marvinlabs/android-intents
20-
// NOTE: Flags
21-
// NO 1: FLAG_ACTIVITY_NEW_TASK:
22-
// When using this flag, if a task is already running for the activity you are now starting,
23-
// then a new activity will not be started; instead,
24-
// the current task will simply be brought to the front of the screen with the state it was last in.
25-
26-
// NOTE: common actions
27-
// NO 2: ACTION_MAIN
28-
// Activity Action Start as a main entry point, does not expect to receive data.
29-
// NO 3: ACTION_VIEW
30-
// This is the most common action performed on a data, usually to view sth.
31-
// NO 4: ACTION_EDIT
32-
// This is the most common action to edit a data, usually to edit some part of intent.
33-
// NO 5: ACTION_DEFAULT
34-
// search on it
35-
36-
// NOTE: you need to use constant value of an action for intent filter
37-
// NOTE: you can find constant value for each action in docs (Ctrl + Q)
38-
// NOTE: Intent Filter Example
39-
// <activity ...>
40-
// <intent-filter>
41-
// <action android:name="android.intent.action.INSERT" />
42-
// <data android:mimeType="vnd.android.cursor.dir/event" />
43-
// <category android:name="android.intent.category.DEFAULT" />
44-
// </intent-filter>
45-
// </activity>
4614
private Context context;
4715
Intent intent;
4816

49-
AlarmIntents(Context context)
17+
private AlarmIntents(Context context)
5018
{
5119
this.context = context;
5220
}
@@ -57,7 +25,7 @@ public static AlarmIntents from(@NonNull Context context)
5725
}
5826

5927
// NOTE: requires com.android.alarm.permission.SET_ALARM permission
60-
public AlarmIntents createAlarmIntent(String message, int hour, int minutes, boolean skipUi)
28+
public AlarmIntents createAlarm(String message, int hour, int minutes, boolean skipUi)
6129
{
6230
// create alarm
6331
intent = new Intent(AlarmClock.ACTION_SET_ALARM);
@@ -70,7 +38,7 @@ public AlarmIntents createAlarmIntent(String message, int hour, int minutes, boo
7038

7139
// NOTE: requires com.android.alarm.permission.SET_ALARM permission
7240
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
73-
public AlarmIntents createAlarmIntent(String message, int hour, int minutes, boolean vibrate, boolean skipUi)
41+
public AlarmIntents createAlarm(String message, int hour, int minutes, boolean vibrate, boolean skipUi)
7442
{
7543
// create alarm
7644
intent = new Intent(AlarmClock.ACTION_SET_ALARM);
@@ -88,7 +56,7 @@ public AlarmIntents createAlarmIntent(String message, int hour, int minutes, boo
8856

8957
// NOTE: requires com.android.alarm.permission.SET_ALARM permission
9058
@RequiresApi(api = Build.VERSION_CODES.M)
91-
public AlarmIntents createAlarmIntent(String message, int hour, int minutes, boolean vibrate, boolean isPm, boolean skipUi)
59+
public AlarmIntents createAlarm(String message, int hour, int minutes, boolean vibrate, boolean isPm, boolean skipUi)
9260
{
9361
// create alarm
9462
intent = new Intent(AlarmClock.ACTION_SET_ALARM);
@@ -105,9 +73,9 @@ public AlarmIntents createAlarmIntent(String message, int hour, int minutes, boo
10573
}
10674

10775
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
108-
public AlarmIntents showAlarmsIntent()
76+
public AlarmIntents showAlarms()
10977
{
110-
Intent intent = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
78+
intent = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
11179
return this;
11280
}
11381

@@ -125,16 +93,8 @@ private void startActivity(Intent intent)
12593
context.startActivity(intent);
12694
}
12795

128-
public boolean show()
96+
public void show()
12997
{
130-
Intent alarmIntent = build();
131-
try
132-
{
133-
startActivity(alarmIntent);
134-
} catch (ActivityNotFoundException e)
135-
{
136-
return false;
137-
}
138-
return true;
98+
startActivity(build());
13999
}
140100
}
Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
package com.next.androidintentlibrary;
22

33
import android.app.Activity;
4-
import android.content.ActivityNotFoundException;
54
import android.content.Context;
65
import android.content.Intent;
76
import android.net.Uri;
7+
88
import androidx.annotation.NonNull;
9-
import android.text.TextUtils;
109

11-
import java.net.URL;
10+
import android.text.TextUtils;
1211

1312
public class BrowserIntents
1413
{
1514
private Context context;
1615
protected Intent intent;
1716

18-
BrowserIntents(Context context)
17+
private BrowserIntents(Context context)
1918
{
2019
this.context = context;
2120
}
@@ -25,7 +24,7 @@ public static BrowserIntents from(@NonNull Context context)
2524
return new BrowserIntents(context);
2625
}
2726

28-
public BrowserIntents openBrowserIntent()
27+
public BrowserIntents openBrowser()
2928
{
3029
intent = new Intent();
3130
intent.setAction(Intent.ACTION_MAIN);
@@ -48,25 +47,12 @@ public BrowserIntents openLink(String url)
4847
return this;
4948
}
5049

51-
public BrowserIntents openLink(URL url)
52-
{
53-
return openLink(url.toString());
54-
}
55-
56-
// TODO: 8/30/2017 How to use URL Http And Https ?
57-
public BrowserIntents LoadWebURL(String http)
50+
public BrowserIntents openLink(Uri uri)
5851
{
59-
Uri webpage = Uri.parse(http);
60-
intent = new Intent(Intent.ACTION_VIEW);
61-
intent.setData(webpage);// TODO: 8/30/2017 This URL can be Http Or Https
62-
intent.setType("text/plain");
63-
intent.setType("text/html");
64-
intent.setType("application/xhtml+xml");
65-
intent.setType("application/vnd.wap.xhtml+xml");
66-
return this;
52+
return openLink(uri.toString());
6753
}
6854

69-
public BrowserIntents openAUrlInBrowser()
55+
public BrowserIntents openGoogle()
7056
{
7157
Uri uri = Uri.parse("https://google.com");
7258
intent = new Intent(Intent.ACTION_VIEW, uri);
@@ -87,16 +73,8 @@ private void startActivity(Intent intent)
8773
context.startActivity(intent);
8874
}
8975

90-
public boolean show()
76+
public void show()
9177
{
92-
Intent browserIntent = build();
93-
try
94-
{
95-
startActivity(browserIntent);
96-
} catch (ActivityNotFoundException e)
97-
{
98-
return false;
99-
}
100-
return true;
78+
startActivity(build());
10179
}
10280
}
Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package com.next.androidintentlibrary;
22

33
import android.app.Activity;
4-
import android.content.ActivityNotFoundException;
54
import android.content.Context;
65
import android.content.Intent;
6+
import android.content.pm.PackageInfo;
7+
import android.content.pm.PackageManager;
8+
79
import androidx.annotation.NonNull;
810

11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
915
public class CalculatorIntents
1016
{
1117
private Context context;
1218
private Intent intent;
19+
private ArrayList<HashMap<String, Object>> items;
20+
private PackageManager packageManager;
1321

14-
CalculatorIntents(Context context)
22+
private CalculatorIntents(Context context)
1523
{
1624
this.context = context;
1725
}
@@ -21,15 +29,46 @@ public static CalculatorIntents from(@NonNull Context context)
2129
return new CalculatorIntents(context);
2230
}
2331

24-
public CalculatorIntents openCalculatorIntent()
32+
// TODO: implement for AppIntents, use same code for other common apps.
33+
public CalculatorIntents openCalculator()
2534
{
35+
getAllPackageNames();
2636
intent = new Intent();
27-
intent.setAction(Intent.ACTION_MAIN);
28-
intent.addCategory(Intent.CATEGORY_APP_CALCULATOR);
29-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
37+
int d = 0;
38+
if (items.size() >= 1)
39+
{
40+
for (int j = 0; j < items.size(); j++)
41+
{
42+
String AppName = (String) items.get(j).get("appName");
43+
// Log.w("Name",""+AppName);
44+
if (AppName.matches("Calculator"))
45+
{
46+
d = j;
47+
break;
48+
}
49+
}
50+
String packageName = (String) items.get(d).get("packageName");
51+
intent = packageManager.getLaunchIntentForPackage(packageName);
52+
}
3053
return this;
3154
}
3255

56+
private void getAllPackageNames()
57+
{
58+
items = new ArrayList<>();
59+
List<PackageInfo> packageInfos;
60+
61+
packageManager = context.getPackageManager();
62+
packageInfos = packageManager.getInstalledPackages(0);
63+
for (PackageInfo pi : packageInfos)
64+
{
65+
HashMap<String, Object> map = new HashMap<String, Object>();
66+
map.put("appName", pi.applicationInfo.loadLabel(packageManager));
67+
map.put("packageName", pi.packageName);
68+
items.add(map);
69+
}
70+
}
71+
3372
public Intent build()
3473
{
3574
return intent;
@@ -44,16 +83,8 @@ private void startActivity(Intent intent)
4483
context.startActivity(intent);
4584
}
4685

47-
public boolean show()
86+
public void show()
4887
{
49-
Intent calculatorIntent = build();
50-
try
51-
{
52-
startActivity(calculatorIntent);
53-
} catch (ActivityNotFoundException e)
54-
{
55-
return false;
56-
}
57-
return true;
88+
startActivity(build());
5889
}
5990
}
Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.next.androidintentlibrary;
22

33
import android.app.Activity;
4-
import android.content.ActivityNotFoundException;
54
import android.content.Context;
65
import android.content.Intent;
6+
77
import androidx.annotation.NonNull;
88

99
public class CalendarIntents
1010
{
1111
private Context context;
1212
private Intent intent;
1313

14-
CalendarIntents(Context context)
14+
private CalendarIntents(Context context)
1515
{
1616
this.context = context;
1717
}
@@ -21,7 +21,7 @@ public static CalendarIntents from(@NonNull Context context)
2121
return new CalendarIntents(context);
2222
}
2323

24-
public CalendarIntents openCalendarIntent()
24+
public CalendarIntents openCalendar()
2525
{
2626
intent = new Intent();
2727
intent.setAction(Intent.ACTION_MAIN);
@@ -30,15 +30,12 @@ public CalendarIntents openCalendarIntent()
3030
return this;
3131
}
3232

33-
public CalendarIntents calendarIntent()
33+
public CalendarIntents createEvent(String title, String description)
3434
{
3535
intent = new Intent(Intent.ACTION_EDIT);
3636
intent.setType("vnd.android.cursor.item/event");
37-
intent.putExtra("title", "Some title");
38-
intent.putExtra("description", "Some description");
39-
intent.putExtra("beginTime", 5000);
40-
intent.putExtra("endTime", 5000);
41-
37+
intent.putExtra("title", title);
38+
intent.putExtra("description", description);
4239
return this;
4340
}
4441

@@ -56,16 +53,8 @@ private void startActivity(Intent intent)
5653
context.startActivity(intent);
5754
}
5855

59-
public boolean show()
56+
public void show()
6057
{
61-
Intent calendarIntent = build();
62-
try
63-
{
64-
startActivity(calendarIntent);
65-
} catch (ActivityNotFoundException e)
66-
{
67-
return false;
68-
}
69-
return true;
58+
startActivity(build());
7059
}
7160
}

0 commit comments

Comments
 (0)