android-Specifying authentication between user and admin - android

I'm writing an android application which will have two types of users.One is admin that s gonna make adding and removing needed stuff and user that is gonna just read only.And I dont know a word about how can i make authentication between them.I need some source and advice.So if I can hear some advice.I really become so happy :)
Thanks in advance.

There would be several approaches to this. One rather quick that gives some flexibility would be to:
1) Create an:
interface ApplicationRights {
boolean writeX()
boolean canSeeButtonY()
}
Then you have a static variable of ApplicationRights interface somewhere accessible from anywhere in the application. (there would be many options for this, this is just a simple way)
To complement this you have two classes:
class AdminRights implements ApplicationRights {
...
}
and
class UserRights implements ApplicationRights {
...
}
Then in that static variable, as soon as you know if it's a User or Admin you
create and put either the UserRights or AdminRights in there. Doing so will allow you to customise this behaviour on a more finegrained level.
And whenever you add a new piece of code that will differ from a User and Admin you can easily add another boolean method in the interface to decide upon the rights. (This would also easily allow you to create more roles than users and admins)
Then to decide and save this to repeat it you can use for example SharedPreferences for knowing which user to create.

Related

Android MVP (Repository pattern) get Camera/Gallery Image?

I am trying to implement MVP architecture using google sample code for mvp. I have an activity as View that has a presenter and model. On button click user can capture and save image in external storage. And on capture click, I also need to play a sound.
I am not sure which code should be put in which class because I cannot put camera capture and play sound code in Activity (which I treat as view) to keep the view as dumb as possible and I cannot put that code in Presenter because it uses Android framework classes (context etc).
So, the only option is to put it in model but in sample code, model only has Repositories (that I believe is relevant to data sources only local/remote).
How to put this code in model and how to link it with other components like View and Presenter? Any guidelines?
Here's one way you can go about solving an issue like that.
Your problem is that some code makes sense to go into a presenter, however it's too Androidy, so you can simply interface around it.
What you basically need is two things that can do certain functions.
interface CapturingSoundPlayer {
void playSound();
}
interface ImageCapturer {
void captureImage();
}
Please note that names and method signatures are up to you and what you need, I am merely using them to make a point.
Now it's perfectly safe for your application to have those two interfaces as dependencies, they have nothing to do with Android, we have simply take then technology out of the equation and only left the behaviour.
You need to pass these dependencies to your presenter and use them whenever needed.
class Presenter {
private final CapturingSoundPlayer soundPlayer;
private final ImageCapturer capturer;
Presenter(CapturingSoundPlayer soundPlayer, ImageCapturer capturer) {
this.soundPlayer = soundPlayer;
this.capturer = capturer;
}
void onCaptureButtonClicked() {
soundPlayer.playSound();
capturer.captureImage();
}
}
Now the implementation for those interfaces can be completely separate from your Activity, making your view still stupid.
Those interfaces (and their implementation) are simply logical units/entities that your presenter uses to split up the logic. I simply think of the functions of "capture" and "playSound" as simple actions, just like view.showLoading, and your presenter plays an orchestrator, it uses "something" to manipulate the view, which is the view interface, and it uses something else to manipulate sound, which is the interface for doing that.
if repositories are considered to be data sources, then in my opinion those helpers don't fit under that definition.
If you have a utility to retrieve the images that has been captured, that would basically can be considered as a repo for your images, but simply taking an image, is just another action.
You can structure/organise them as you feel fit, but just because you are using MVP, not every class you create has to be either one of those letters (M/V/P).
Sometimes any of those layers will need classes that logical units to do something, so that you have better separation of concerns.
Consider you have quite complex formatting logic, it would make sense to separate that in a separate class other than the presenter, but does that mean that the separate class is now model? It could, but it doesn't have to be.

Activity is getting too large and becoming more and more difficult to work with. Solutions to solve this issue?

I am working on my second Android Application, first being, hello world. The application code is quite crazy looking because I love to test new libraries and ideas in it. I have been working on this application for well over 3 months and one of my activities is getting way to large and difficult to work with. I find myself getting lost in the code and it is taking longer to do simple things. There might be simple solutions to solving this issue. I really want to split my activity into two and reference each other if possible. Is there are any suggestions to simplifying and organizing code that would be greatly helpful. Even example will help me very much.
Part of my activity is adding a ton of data into a database and the other part is a long equation with multiple values. Another part is implementing the HoloGraphLibrary (Which I love). It is also implementing a listView with custom adapter. It also has a custom dialog............ I can go on and on. I hope you get my point.
EDIT
Going to work with this.
HoloGraphHelper holoGraph = new HoloGraphHelper();
holoGraph.initialize();
Try creating classes for each responsibility.
A Database Helper that has functions to insert data too:
DatabaseHelper database = new DatabaseHelper();
database .insertData(whatever);
A HoloGraphHelper that initializes the HoloGraph
HoloGraphHelper holoGraph = new HoloGraphHelper();
holoGraph.initialize();
And so on.
Break into multiple files. First classes defined in the Activity like the adapter. Change anonymous classes to classes defined in their own file. Look for ways to break out other related code into a class.
Right click on src folder of your Project and select new - class to create a new class. You can use a class for storing methods but you won't be able to display anything on screen.
To display contents to user, you can create a new Activity bu pressing Ctrl + N and selecting Android - Android Activity.
The best way is modularise your code.
I.e split your code into various related modules, for example a separate class for each part that your testing. So you could have a database entry class, a class for Gui testing, i.e. for your custom dialog. That class does all the work for that test, into various functions, I always try to keep functions as small as possible as they are easy to read.
As an example for your database entry, you could have a function which checks the database if the record already exists and then insert it. But a better way would be your insert function only performs the insert code and instead within this function it calls CheckIfDatAlreadyExists function which can return a bool so you know whether you should go ahead and insert the record. This would keep the code tidy and clean to manage.
Then from your main activity all would need to do is instantiate the relative class and call the relevant method.

Add user defined place in the input field by clicking a button to stop the geocomplete

My requirement is to put in a place name in a text field and show that in the map, so i used geocomplete js, which works well.
Now my user should be able to put in user defined places like 'my house', for that I need to remove the geocomplete on clicking a 'x' button on top of the map.
How can I implement this?
Thanks in advance
I wouldn't customize the package! When a new version comes out, you'll have to make the same changes you before.
Since you haven't provided any code, I can give you an idea of what I've done with JQuery validation method overrides.
You'll simply have to find the listener (something like $('#listenToThis').on('click', function(){ doThings(); }) in the geocomplete.js file, then override it in a file that is included after geocomplete.
If you're using bundles, just include your file after the geocomplete listener response is defined.
So, after you find those, you can do something similar to the following:
$.validator.methods.number = function (value, element) {
value = floatValue(value);
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}
The function above allowed me to validate client-side numbers that were formatted as currency in JQuery ($).
This overrides the JQuery.validator.methods.number function (a cheating way to override the function without changing the package code), so if you can pinpoint the geocomplete.addSomethingToMap or geocomplete.reactToClick function, you should override it and it will essentially work that way.
Warning, though: you will need to reinclude the changes when you want to reenable the feature. You'll have to override, override, override again. This may not be the best way if they're going to be adding hundreds of different new locations on one screen, but for up to a small unit, such as a dozen, it should be a good solution.

How to pass Views between Classes

I've been working on android for few months, during my spare time, so probably i'm going to ask stupid questions.
In a class, I create an object, for example a button, and I'd like to 'reach' it from others classes.
Of course i could do it this way:
public class CreateButton{
public void createButton(){
Button myButton = new Button(context);
//I can "pass" my object to another class this way
ManageButton manageButton = new ManageButton(myButton);
// or this way
manageButton.writeButtonTextMethod(myButton);
}
}
or this way
public class CreateButton{
public Button createButton(){
Button myButton = new Button(context);
return myButton;
}
}
public class ManageButton{
public void writeButtonTextMethod(){
CreateButton createButton = new CreateButton()
Button myButton = createButton.createButton();
myButton.setText("W");
}
}
I'm wondering if there's a way to 'reach' myButton, created in createButton.class directly from ManageButton.class (and other classes).
With the code above, I have to 'call' createButton.class from ManageButton.class or vice versa to be allowed to manage myButton.
I can do it making myButton static, but it's not correct make view static.
For example, i can easily reach variables created in a class that extends Application. Is there something similar for views?
Much people/guides/articles claim is possible and correct sharing objects (and fields) between activities by using Application, this way:
Such people are idiots, as myButton will introduce a memory leak unless this is done very carefully.
So, I'm still not sure how i should share objects (TextView, Button, ImgaeView etc.) among different activities.
You don't share widgets between activities. At most, you share model data between activities (or, better yet, pointers to centrally-stored model data).
In a Web app, you do not share DOM nodes between Web pages, as that is not possible. At most, you share data passed as GET parameters and the like between Web pages, or you store data in local central storage (cookies, local storage, IndexedDB, etc.). Android activities are like Web pages -- they are loosely coupled.
If you have pieces of your UI that are so tightly coupled that they absolutely need to share widgets, they should not be separate activities.
Views are not meant to be shared across Activities. Even if you do disregard this and share them, you're more than likely to run into a WindowManager$BadTokenException, which essentially means you can't alter Views outside of its parent Activity.
However, to answer your question in a more general way about sharing non-view objects, the Application class is the right way to go. However, making the objects static is not the right way to do. Instead, make them instance variables, and access them in in your various activities by using:
Application application:
//Below line is in onCreate()
application = this.getApplication();
After that, simple access it using
application.myObject; //Or you could use getters and setters. Your choice.
Not quite sure why would u like to share one button between two activities?!
It is either button in ActivityA or ActivityB? No need, or use to do it..You shouldn't do it.
EDIT: This is more Java Basics topic then Android.. And from what I understood, u call "class" something that IMHO is method (because u can't CALL the class and because what did you described).
Best advices I can give u here:
Study Java basics (methods, classes, objects, constructors, inheritance..),
Study Android basics (simple examples with 1 activity til u understand it good),
Make MyButton Class which will extend standard button,
Create button(s) and change their properties
This few tasks will make u busy for some time. Take it easy because it will be easier later with complicated apps. Hope I helped you. Cheers
EDIT2: In last example u wrote (in comments to this answer), nothing make sense because as we wrote u before u don't share button between two activities. Pause! Read it again: You should't share button between two Activies. Questions u asked are not clear because for A and Z u can't say just class and "share" button between them. Which class? no line of code written so it is really difficult to figure out what u want.. :s
LAST LAST EDIT: Man.. Please understand that u r missing basics of Java. No bad intentions here, just try to guide u and tell u exactly what u should study. Why? Because it doesn't surprise me that you are confused to explain what u mean when classes are with one method named exactly the same.. They are just objects "doing something", u want to "manage" them by adding variable in constructor which doesn't exist etc.. Learn, next time write immediately code to save time for all of us. Cheers and enjoy learning

Android synchronization (I think)

I am new to Android and have learned a lot with this project. I am almost there but, but one of my missing parts is... the game does something like the Simon Says game, and right now I am able to click on the Images and it also loops through them for the user but I can touch them while the loop goes through them. I want now to be able to set like a boolean flag that says userTurn if 0 then it's user turn and if 1 then its the system. Something of that sort.
What would be the best way to approach this?
If the various states in which your application can be in is very large and/or complex, I would recommend the state pattern. But if you just need two states, either system or user, I would use something like this:
private boolean isSystemTurn = false;
and then check it like that:
if (isSystemTurn) {
// the user has to wait
} else {
// it is user turn
}

Categories

Resources