Skip to content

Commit 96716be

Browse files
authored
Merge pull request #138 from brarcher/shortcuts
Move shortcut creation to launcher
2 parents 53bc249 + 95bdc8d commit 96716be

File tree

6 files changed

+182
-49
lines changed

6 files changed

+182
-49
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package="protect.budgetwatch">
44

55
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6-
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
76
<uses-permission android:name="android.permission.CAMERA"/>
87
<uses-feature android:required="true" android:name="android.hardware.camera"/>
98

@@ -68,6 +67,15 @@
6867
android:name=".intro.IntroActivity"
6968
android:label=""
7069
android:theme="@style/AppTheme.NoActionBar"/>
70+
<activity
71+
android:name=".ShortcutConfigure"
72+
android:label="@string/addShortcutsTitle"
73+
android:configChanges="orientation|screenSize">
74+
<intent-filter>
75+
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
76+
<category android:name="android.intent.category.DEFAULT"/>
77+
</intent-filter>
78+
</activity>
7179
<receiver android:label="@string/app_name" android:name="TransactionExpenseWidget">
7280
<intent-filter>
7381
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>

app/src/main/java/protect/budgetwatch/MainActivity.java

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -182,53 +182,9 @@ public boolean onOptionsItemSelected(MenuItem item)
182182
return true;
183183
}
184184

185-
if(id == R.id.action_shortcuts)
186-
{
187-
addShortcuts();
188-
return true;
189-
}
190-
191185
return super.onOptionsItemSelected(item);
192186
}
193187

194-
private void addShortcuts()
195-
{
196-
for(int transactionType : new int[]{DBHelper.TransactionDbIds.EXPENSE, DBHelper.TransactionDbIds.REVENUE})
197-
{
198-
Intent shortcutIntent = new Intent(this, TransactionViewActivity.class);
199-
shortcutIntent.setAction(Intent.ACTION_MAIN);
200-
// Prevent instances of the view activity from piling up; if one exists let this
201-
// one replace it.
202-
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
203-
Bundle bundle = new Bundle();
204-
bundle.putInt("type", transactionType);
205-
shortcutIntent.putExtras(bundle);
206-
207-
Intent intent = new Intent();
208-
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
209-
210-
String title;
211-
if(transactionType == DBHelper.TransactionDbIds.EXPENSE)
212-
{
213-
title = getResources().getString(R.string.addExpenseTransactionTitle);
214-
}
215-
else
216-
{
217-
title = getResources().getString(R.string.addRevenueTransactionTitle);
218-
}
219-
220-
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
221-
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
222-
.fromContext(this, R.mipmap.ic_launcher));
223-
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
224-
// Do not duplicate the shortcut if it is already there
225-
intent.putExtra("duplicate", false);
226-
getApplicationContext().sendBroadcast(intent);
227-
}
228-
229-
Toast.makeText(this, R.string.shortcutsAdded, Toast.LENGTH_LONG).show();
230-
}
231-
232188
private void displayAboutDialog()
233189
{
234190
final Map<String, String> USED_LIBRARIES = ImmutableMap.of
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package protect.budgetwatch;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.os.Parcelable;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.support.v7.widget.Toolbar;
9+
import android.util.Log;
10+
import android.view.LayoutInflater;
11+
import android.view.View;
12+
import android.view.ViewGroup;
13+
import android.widget.AdapterView;
14+
import android.widget.ArrayAdapter;
15+
import android.widget.ListView;
16+
import android.widget.TextView;
17+
18+
import java.util.LinkedList;
19+
import java.util.List;
20+
21+
public class ShortcutConfigure extends AppCompatActivity
22+
{
23+
static final String TAG = "BudgetWatch";
24+
25+
static class ShortcutOption
26+
{
27+
String name;
28+
Intent intent;
29+
}
30+
31+
static class ShortcutAdapter extends ArrayAdapter<ShortcutOption>
32+
{
33+
ShortcutAdapter(Context context, List<ShortcutOption> items)
34+
{
35+
super(context, 0, items);
36+
}
37+
38+
static class ViewHolder
39+
{
40+
TextView name;
41+
}
42+
43+
@Override
44+
public View getView(int position, View convertView, ViewGroup parent)
45+
{
46+
// Get the data item for this position
47+
ShortcutOption item = getItem(position);
48+
49+
ShortcutAdapter.ViewHolder holder;
50+
51+
// Check if an existing view is being reused, otherwise inflate the view
52+
53+
if (convertView == null)
54+
{
55+
convertView = LayoutInflater.from(getContext()).inflate(R.layout.shortcut_option_layout,
56+
parent, false);
57+
58+
holder = new ShortcutAdapter.ViewHolder();
59+
holder.name = (TextView) convertView.findViewById(R.id.name);
60+
convertView.setTag(holder);
61+
}
62+
else
63+
{
64+
holder = (ShortcutAdapter.ViewHolder)convertView.getTag();
65+
}
66+
67+
holder.name.setText(item.name);
68+
69+
return convertView;
70+
}
71+
}
72+
73+
private List<ShortcutOption> getPossibleShortcuts()
74+
{
75+
LinkedList<ShortcutOption> shortcuts = new LinkedList<>();
76+
77+
for(int transactionType : new int[]{DBHelper.TransactionDbIds.EXPENSE, DBHelper.TransactionDbIds.REVENUE})
78+
{
79+
Intent shortcutIntent = new Intent(this, TransactionViewActivity.class);
80+
shortcutIntent.setAction(Intent.ACTION_MAIN);
81+
// Prevent instances of the view activity from piling up; if one exists let this
82+
// one replace it.
83+
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
84+
Bundle bundle = new Bundle();
85+
bundle.putInt("type", transactionType);
86+
shortcutIntent.putExtras(bundle);
87+
88+
String title;
89+
if(transactionType == DBHelper.TransactionDbIds.EXPENSE)
90+
{
91+
title = getResources().getString(R.string.addExpenseTransactionShortcutTitle);
92+
}
93+
else
94+
{
95+
title = getResources().getString(R.string.addRevenueTransactionShortcutTitle);
96+
}
97+
98+
ShortcutOption shortcutOption = new ShortcutOption();
99+
shortcutOption.name = title;
100+
shortcutOption.intent = shortcutIntent;
101+
102+
shortcuts.add(shortcutOption);
103+
}
104+
105+
return shortcuts;
106+
}
107+
108+
@Override
109+
public void onCreate(Bundle bundle)
110+
{
111+
super.onCreate(bundle);
112+
113+
// Set the result to CANCELED. This will cause nothing to happen if the
114+
// aback button is pressed.
115+
setResult(RESULT_CANCELED);
116+
117+
setContentView(R.layout.main_activity);
118+
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
119+
toolbar.setVisibility(View.GONE);
120+
121+
final ListView shortcutList = (ListView) findViewById(R.id.list);
122+
shortcutList.setVisibility(View.VISIBLE);
123+
124+
final ShortcutAdapter adapter = new ShortcutAdapter(this, getPossibleShortcuts());
125+
shortcutList.setAdapter(adapter);
126+
127+
shortcutList.setOnItemClickListener(new AdapterView.OnItemClickListener()
128+
{
129+
@Override
130+
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
131+
{
132+
ShortcutOption shortcut = (ShortcutOption)parent.getItemAtPosition(position);
133+
if(shortcut == null)
134+
{
135+
Log.w(TAG, "Clicked shortcut at position " + position + " is null");
136+
return;
137+
}
138+
139+
Parcelable icon = Intent.ShortcutIconResource.fromContext(ShortcutConfigure.this, R.mipmap.ic_launcher);
140+
Intent intent = new Intent();
141+
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut.intent);
142+
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcut.name);
143+
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
144+
setResult(RESULT_OK, intent);
145+
146+
finish();
147+
}
148+
});
149+
}
150+
}
151+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout android:orientation="vertical"
3+
android:padding="10.0dp"
4+
android:layout_width="fill_parent"
5+
android:layout_height="fill_parent"
6+
xmlns:android="http://schemas.android.com/apk/res/android">
7+
<LinearLayout android:orientation="horizontal"
8+
android:padding="5.0dp"
9+
android:layout_width="fill_parent"
10+
android:layout_height="wrap_content"
11+
android:baselineAligned="true">
12+
<TextView android:textSize="20.0sp"
13+
android:id="@+id/name"
14+
android:layout_width="0dp"
15+
android:layout_height="wrap_content"
16+
android:layout_weight="1.0"
17+
android:maxLines="1"/>
18+
</LinearLayout>
19+
</LinearLayout>

app/src/main/res/menu/main_menu.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
android:icon="@drawable/ic_import_export_white_24dp"
1313
android:title="@string/importExport"
1414
app:showAsAction="ifRoom"/>
15-
<item
16-
android:id="@+id/action_shortcuts"
17-
android:title="@string/addShortcuts"
18-
app:showAsAction="never"/>
1915
<item
2016
android:id="@+id/action_intro"
2117
android:title="@string/startIntro"

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<string name="viewRevenueTransactionTitle">View Revenue</string>
1919
<string name="importExportTitle">Import/Export</string>
2020
<string name="totalBudgetTitle">Total</string>
21+
<string name="addShortcutsTitle">Add Budget Watch Shortcut</string>
22+
<string name="addExpenseTransactionShortcutTitle">Add Expense Shortcut</string>
23+
<string name="addRevenueTransactionShortcutTitle">Add Revenue Shortcut</string>
2124

2225
<string name="add">Add</string>
2326
<string name="selectDates">Select Dates</string>

0 commit comments

Comments
 (0)