Переглянути джерело

desktop shortcut

tags/0.0.5
Robin Thoni 9 роки тому
джерело
коміт
5d7f429327

+ 22
- 3
app/src/main/AndroidManifest.xml Переглянути файл

@@ -15,9 +15,9 @@
15 15
         android:supportsRtl="true"
16 16
         android:theme="@style/AppTheme">
17 17
         <activity
18
-            android:screenOrientation="portrait"
19 18
             android:name=".ui.MainActivity"
20 19
             android:label="@string/app_name"
20
+            android:screenOrientation="portrait"
21 21
             android:theme="@style/AppTheme.NoActionBar">
22 22
             <intent-filter>
23 23
                 <action android:name="android.intent.action.MAIN"/>
@@ -40,10 +40,29 @@
40 40
                 android:resource="@xml/authenticator"/>
41 41
         </service>
42 42
 
43
-        <activity android:name=".ui.AddAccountActivity"
44
-            android:label="@string/add_location_title">
43
+        <activity
44
+            android:name=".ui.AddAccountActivity"
45
+            android:label="@string/add_location_title"
46
+            android:screenOrientation="portrait">
47
+        </activity>
48
+
49
+        <activity
50
+            android:name=".ui.AddCommandShortcutActivity"
51
+            android:label="@string/add_shortcut_title"
52
+            android:launchMode="singleInstance"
53
+            android:icon="@mipmap/ic_command_bg"
54
+            android:screenOrientation="portrait">
55
+            <intent-filter>
56
+                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
57
+            </intent-filter>
45 58
         </activity>
46 59
 
60
+        <activity android:name=".ui.CommandExecActivity"
61
+                  android:launchMode="singleInstance">
62
+            <intent-filter>
63
+                <action android:name="android.intent.action.MAIN"/>
64
+            </intent-filter>
65
+        </activity>
47 66
     </application>
48 67
 
49 68
 </manifest>

BIN
app/src/main/ic_command_bg-web.png Переглянути файл


+ 6
- 1
app/src/main/java/com/rthoni/camotion/business/CommandsBusiness.java Переглянути файл

@@ -23,8 +23,13 @@ public class CommandsBusiness extends LuBusinessManager {
23 23
         return CommandsDataAccess.getAll(config, page, perPage);
24 24
     }
25 25
 
26
+    public static LuPromise<LuBoolDbo> exec(LuDataAccessConfigDbo config, int commandId)
27
+    {
28
+        return CommandsDataAccess.exec(config, commandId);
29
+    }
30
+
26 31
     public static LuPromise<LuBoolDbo> exec(LuDataAccessConfigDbo config, CommandDbo command)
27 32
     {
28
-        return CommandsDataAccess.exec(config, command.getId());
33
+        return exec(config, command.getId());
29 34
     }
30 35
 }

+ 5
- 0
app/src/main/java/com/rthoni/camotion/dbo/LocationDbo.java Переглянути файл

@@ -107,4 +107,9 @@ public class LocationDbo extends LuDbo {
107 107
         config.setBaseUrl(getBaseUrl());
108 108
         return config;
109 109
     }
110
+
111
+    public String getPseudoMail()
112
+    {
113
+        return getUsername() + "@" + getName();
114
+    }
110 115
 }

+ 1
- 1
app/src/main/java/com/rthoni/camotion/ui/AddAccountActivity.java Переглянути файл

@@ -101,7 +101,7 @@ public class AddAccountActivity extends AppCompatActivity {
101 101
 
102 102
     private void addAccount(LocationDbo location)
103 103
     {
104
-        String username = location.getUsername() + "@" + location.getName();
104
+        String username = location.getPseudoMail();
105 105
 
106 106
         AccountManager accountManager = AccountManager.get(this);
107 107
         Account newUserAccount = new Account(username, getResources().getString(R.string.account_type));

+ 89
- 0
app/src/main/java/com/rthoni/camotion/ui/AddCommandShortcutActivity.java Переглянути файл

@@ -0,0 +1,89 @@
1
+package com.rthoni.camotion.ui;
2
+
3
+import android.app.Activity;
4
+import android.content.Intent;
5
+import android.os.Bundle;
6
+import android.widget.Toast;
7
+
8
+import com.luticate.auth.dbo.LuFullLoginDbo;
9
+import com.luticate.auth.dbo.LuLoginDbo;
10
+import com.luticate.utils.business.LuPromise;
11
+import com.luticate.utils.business.LuRequest;
12
+import com.rthoni.camotion.R;
13
+import com.rthoni.camotion.dbo.CommandDbo;
14
+import com.rthoni.camotion.dbo.LocationDbo;
15
+import com.rthoni.camotion.ui.dialogs.LocationPickerDialog;
16
+import com.rthoni.camotion.ui.dialogs.LoginDialog;
17
+import com.rthoni.camotion.ui.fragments.AbstractCommandsFragment;
18
+
19
+public class AddCommandShortcutActivity extends Activity {
20
+
21
+    @Override
22
+    protected void onCreate(Bundle savedInstanceState) {
23
+        super.onCreate(savedInstanceState);
24
+        LuRequest.init(this);
25
+        setContentView(R.layout.content_main);
26
+        selectLocation();
27
+    }
28
+
29
+    private void selectLocation()
30
+    {
31
+        LocationPickerDialog.getLocation(this)
32
+                .then(new LuPromise.LuConsumer<LocationDbo>() {
33
+                    @Override
34
+                    public void execute(LocationDbo location) {
35
+                        login(location);
36
+                    }
37
+                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
38
+                    @Override
39
+                    public void execute(LuPromise.LuPromiseError data) {
40
+                        setResult(RESULT_CANCELED);
41
+                        finish();
42
+                    }
43
+                });
44
+    }
45
+
46
+    private void login(final LocationDbo location)
47
+    {
48
+        LoginDialog dlg = new LoginDialog(this);
49
+        dlg.loginFull(location).then(new LuPromise.LuConsumer<LuFullLoginDbo>() {
50
+            @Override
51
+            public void execute(LuFullLoginDbo loginDbo) {
52
+                showCommands(loginDbo, location);
53
+            }
54
+        }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
55
+            @Override
56
+            public void execute(LuPromise.LuPromiseError error) {
57
+                selectLocation();
58
+                Toast.makeText(AddCommandShortcutActivity.this, error.toString(), Toast.LENGTH_LONG).show();
59
+            }
60
+        });
61
+    }
62
+
63
+    private void showCommands(final LuFullLoginDbo loginDbo, final LocationDbo location)
64
+    {
65
+        AbstractCommandsFragment fragment = new AbstractCommandsFragment() {
66
+            @Override
67
+            protected void onCommandClicked(CommandDbo command) {
68
+                addShortcut(location, command);
69
+            }
70
+        };
71
+        fragment.setCamotionDbo(loginDbo, location);
72
+        getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
73
+    }
74
+
75
+    private void addShortcut(LocationDbo location, CommandDbo command)
76
+    {
77
+        Intent shortcutIntent = new Intent(this, CommandExecActivity.class);
78
+        shortcutIntent.putExtra("LOCATION_PSEUDO_MAIL", location.getPseudoMail());
79
+        shortcutIntent.putExtra("COMMAND", command.toString());
80
+        Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_command_bg);
81
+
82
+        Intent intent = new Intent();
83
+        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
84
+        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, command.getName());
85
+        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
86
+        setResult(RESULT_OK, intent);
87
+        finish();
88
+    }
89
+}

+ 121
- 0
app/src/main/java/com/rthoni/camotion/ui/CommandExecActivity.java Переглянути файл

@@ -0,0 +1,121 @@
1
+package com.rthoni.camotion.ui;
2
+
3
+import android.app.Activity;
4
+import android.content.DialogInterface;
5
+import android.os.Bundle;
6
+import android.support.v7.app.AlertDialog;
7
+import android.widget.Toast;
8
+
9
+import com.luticate.auth.dbo.LuFullLoginDbo;
10
+import com.luticate.auth.dbo.LuLoginDbo;
11
+import com.luticate.utils.business.LuPromise;
12
+import com.rthoni.camotion.R;
13
+import com.rthoni.camotion.business.CamotionBusiness;
14
+import com.rthoni.camotion.dbo.CommandDbo;
15
+import com.rthoni.camotion.dbo.LocationDbo;
16
+import com.rthoni.camotion.ui.dialogs.CommandExecDialog;
17
+import com.rthoni.camotion.ui.dialogs.LoginDialog;
18
+
19
+import org.json.JSONException;
20
+import org.json.JSONObject;
21
+
22
+import java.util.List;
23
+
24
+public class CommandExecActivity extends Activity {
25
+
26
+    @Override
27
+    protected void onCreate(Bundle savedInstanceState) {
28
+        super.onCreate(savedInstanceState);
29
+        setContentView(R.layout.activity_command_exec);
30
+
31
+        CommandDbo command = new CommandDbo();
32
+        try {
33
+            JSONObject commandJson = new JSONObject(getIntent().getStringExtra("COMMAND"));
34
+            command.fromJson(commandJson);
35
+        } catch (Exception e) {
36
+            e.printStackTrace();
37
+            setResult(RESULT_CANCELED);
38
+            finish();
39
+            return;
40
+        }
41
+
42
+        LocationDbo location = getLocation();
43
+        if (location == null) {
44
+            setResult(RESULT_CANCELED);
45
+            finish();
46
+            return;
47
+        }
48
+
49
+        askConfirmation(command, location);
50
+    }
51
+
52
+    private LocationDbo getLocation()
53
+    {
54
+        String locationPseudoMail = getIntent().getStringExtra("LOCATION_PSEUDO_MAIL");
55
+        List<LocationDbo> locations = CamotionBusiness.getLocations(this);
56
+        for (LocationDbo location : locations) {
57
+            if (location.getPseudoMail().equals(locationPseudoMail)) {
58
+                return location;
59
+            }
60
+        }
61
+        return null;
62
+    }
63
+
64
+    private void askConfirmation(final CommandDbo command, final LocationDbo location)
65
+    {
66
+        AlertDialog.Builder builder = new AlertDialog.Builder(this);
67
+        builder.setMessage(this.getString(R.string.command_confirm, command.getName()))
68
+                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
69
+                    @Override
70
+                    public void onClick(DialogInterface dialog, int which) {
71
+                        login(command, location);
72
+                    }
73
+                })
74
+                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
75
+                    @Override
76
+                    public void onClick(DialogInterface dialog, int which) {
77
+                        setResult(RESULT_CANCELED);
78
+                        finish();
79
+                    }
80
+                })
81
+                .setOnCancelListener(new DialogInterface.OnCancelListener() {
82
+                    @Override
83
+                    public void onCancel(DialogInterface dialog) {
84
+                        setResult(RESULT_CANCELED);
85
+                        finish();
86
+                    }
87
+                })
88
+                .show();
89
+    }
90
+
91
+    private void login(final CommandDbo command, final LocationDbo location)
92
+    {
93
+        LoginDialog dlg = new LoginDialog(this);
94
+        dlg.login(location).then(new LuPromise.LuConsumer<LuLoginDbo>() {
95
+            @Override
96
+            public void execute(LuLoginDbo loginDbo) {
97
+                CommandExecDialog.showLoadingDialog(location.getConfig(loginDbo), CommandExecActivity.this, command)
98
+                .then(new LuPromise.LuConsumer<Boolean>() {
99
+                    @Override
100
+                    public void execute(Boolean data) {
101
+                        setResult(RESULT_OK);
102
+                        finish();
103
+                    }
104
+                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
105
+                    @Override
106
+                    public void execute(LuPromise.LuPromiseError error) {
107
+                        setResult(RESULT_CANCELED);
108
+                        finish();
109
+                    }
110
+                });
111
+            }
112
+        }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
113
+            @Override
114
+            public void execute(LuPromise.LuPromiseError error) {
115
+                Toast.makeText(CommandExecActivity.this, error.toString(), Toast.LENGTH_LONG).show();
116
+                setResult(RESULT_CANCELED);
117
+                finish();
118
+            }
119
+        });
120
+    }
121
+}

+ 36
- 8
app/src/main/java/com/rthoni/camotion/ui/dialogs/CommandExecDialog.java Переглянути файл

@@ -39,8 +39,9 @@ public abstract class CommandExecDialog {
39 39
                 .show();
40 40
     }
41 41
 
42
-    protected static void showLoadingDialog(final LuDataAccessConfigDbo config, final Context context, final CommandDbo command)
42
+    public static LuPromise<Boolean> showLoadingDialog(final LuDataAccessConfigDbo config, final Context context, final CommandDbo command)
43 43
     {
44
+        final LuPromise<Boolean> promise = new LuPromise<>();
44 45
         final ProgressDialog dlg = new ProgressDialog(context);
45 46
         dlg.setIndeterminate(true);
46 47
         dlg.setTitle(R.string.loading);
@@ -51,22 +52,49 @@ public abstract class CommandExecDialog {
51 52
             @Override
52 53
             public void execute(LuBoolDbo data) {
53 54
                 dlg.hide();
54
-                showEndDialog(context, context.getString(R.string.command_success, command.getName()));
55
+                showEndDialog(context, context.getString(R.string.command_success, command.getName()))
56
+                    .then(new LuPromise.LuConsumer<Boolean>() {
57
+                        @Override
58
+                        public void execute(Boolean data) {
59
+                            promise.resolve(null);
60
+                        }
61
+                    });
55 62
             }
56 63
         }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
57 64
             @Override
58
-            public void execute(LuPromise.LuPromiseError error) {
65
+            public void execute(final LuPromise.LuPromiseError error) {
59 66
                 dlg.hide();
60
-                showEndDialog(context, context.getString(R.string.error, error.toString()));
67
+                showEndDialog(context, context.getString(R.string.error, error.toString()))
68
+                        .then(new LuPromise.LuConsumer<Boolean>() {
69
+                            @Override
70
+                            public void execute(Boolean data) {
71
+                                promise.reject(error);
72
+                            }
73
+                        });
61 74
             }
62 75
         });
76
+        return promise;
63 77
     }
64 78
 
65
-    protected static void showEndDialog(Context context, String text)
79
+    protected static LuPromise<Boolean> showEndDialog(Context context, String text)
66 80
     {
81
+        final LuPromise<Boolean> promise = new LuPromise<>();
67 82
         AlertDialog.Builder builder = new AlertDialog.Builder(context);
68
-        builder.setMessage(text);
69
-        builder.setPositiveButton(android.R.string.ok, null);
70
-        builder.show();
83
+        builder.setMessage(text)
84
+                .setCancelable(true)
85
+                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
86
+                    @Override
87
+                    public void onClick(DialogInterface dialog, int which) {
88
+                        promise.resolve(null);
89
+                    }
90
+                })
91
+                .setOnCancelListener(new DialogInterface.OnCancelListener() {
92
+                    @Override
93
+                    public void onCancel(DialogInterface dialog) {
94
+                        promise.resolve(null);
95
+                    }
96
+                })
97
+                .show();
98
+        return promise;
71 99
     }
72 100
 }

+ 46
- 0
app/src/main/java/com/rthoni/camotion/ui/dialogs/LocationPickerDialog.java Переглянути файл

@@ -0,0 +1,46 @@
1
+package com.rthoni.camotion.ui.dialogs;
2
+
3
+import android.app.AlertDialog;
4
+import android.content.Context;
5
+import android.content.DialogInterface;
6
+
7
+import com.luticate.utils.business.LuPromise;
8
+import com.rthoni.camotion.R;
9
+import com.rthoni.camotion.business.CamotionBusiness;
10
+import com.rthoni.camotion.dbo.LocationDbo;
11
+
12
+import java.util.List;
13
+
14
+/**
15
+ * Created by robin on 12/10/15.
16
+ */
17
+public abstract class LocationPickerDialog {
18
+    public static LuPromise<LocationDbo> getLocation(Context context)
19
+    {
20
+        final LuPromise<LocationDbo> promise = new LuPromise<>();
21
+
22
+        final List<LocationDbo> locations = CamotionBusiness.getLocations(context);
23
+
24
+        String[] locationStrings = new String[locations.size()];
25
+        for (int i = 0; i < locations.size(); ++i) {
26
+            LocationDbo location = locations.get(i);
27
+            locationStrings[i] = location.getPseudoMail();
28
+        }
29
+
30
+        AlertDialog.Builder builder = new AlertDialog.Builder(context);
31
+        builder.setTitle(R.string.locations)
32
+                .setItems(locationStrings, new DialogInterface.OnClickListener() {
33
+                    public void onClick(DialogInterface dialog, int which) {
34
+                        promise.resolve(locations.get(which));
35
+                    }
36
+                })
37
+                .setOnCancelListener(new DialogInterface.OnCancelListener() {
38
+                    @Override
39
+                    public void onCancel(DialogInterface dialog) {
40
+                        promise.reject(null);
41
+                    }
42
+                })
43
+        .show();
44
+        return promise;
45
+    }
46
+}

+ 13
- 0
app/src/main/res/layout/activity_command_exec.xml Переглянути файл

@@ -0,0 +1,13 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<RelativeLayout
3
+    xmlns:android="http://schemas.android.com/apk/res/android"
4
+    xmlns:tools="http://schemas.android.com/tools"
5
+    android:layout_width="match_parent"
6
+    android:layout_height="match_parent"
7
+    android:paddingBottom="@dimen/activity_vertical_margin"
8
+    android:paddingLeft="@dimen/activity_horizontal_margin"
9
+    android:paddingRight="@dimen/activity_horizontal_margin"
10
+    android:paddingTop="@dimen/activity_vertical_margin"
11
+    tools:context="com.rthoni.camotion.ui.CommandExecActivity">
12
+
13
+</RelativeLayout>

BIN
app/src/main/res/mipmap-hdpi/ic_command_bg.png Переглянути файл


BIN
app/src/main/res/mipmap-mdpi/ic_command_bg.png Переглянути файл


BIN
app/src/main/res/mipmap-xhdpi/ic_command_bg.png Переглянути файл


BIN
app/src/main/res/mipmap-xxhdpi/ic_command_bg.png Переглянути файл


BIN
app/src/main/res/mipmap-xxxhdpi/ic_command_bg.png Переглянути файл


+ 1
- 0
app/src/main/res/values/strings.xml Переглянути файл

@@ -5,6 +5,7 @@
5 5
     <string name="error">Error : %1$s</string>
6 6
 
7 7
     <string name="add_location_title">Add Camotion Account</string>
8
+    <string name="add_shortcut_title">Camotion - Shortcut</string>
8 9
 
9 10
     <string name="navigation_drawer_open">Open navigation drawer</string>
10 11
     <string name="navigation_drawer_close">Close navigation drawer</string>

Завантаження…
Відмінити
Зберегти