Is not an enclosing class Java Interface - java

I am trying to make a fan made of an app but when I want to create a constructor with the AsyncRun interface class it gives me 2 errors that it is not a class and I am stuck on that
This is all the code of MultiThreadHelper
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import com.bluesquare.center.provider.MultiThreadHelper;
public class MultiThreadHelper {
public interface AsyncRun<T> {
void onError(Throwable th);
T onExecute();
void onSuccess(T t);
}
public static class Helper {
private static final Helper instance = new Helper();
private Handler handler;
private Handler mainHandler;
private Helper() {
HandlerThread handlerThread = new HandlerThread("MutiThreadHelper");
handlerThread.start();
this.handler = new Handler(handlerThread.getLooper());
this.mainHandler = new Handler(Looper.getMainLooper());
}
public void post(Runnable runnable) {
this.handler.post(runnable);
}
public void postOnMainThread(Runnable runnable) {
this.mainHandler.post(runnable);
}
public void a(final AsyncRun asyncRun) {
try {
final Object onExecute = asyncRun.onExecute();
this.mainHandler.post(new Runnable() {
#Override
public final void run() {
MultiThreadHelper.AsyncRun.this.onSuccess(onExecute);
}
});
} catch (Exception e2) {
e2.printStackTrace();
this.mainHandler.post(new Runnable() {
#Override
public final void run() {
MultiThreadHelper.AsyncRun.this.onError(e2);
}
});
}
}
public <T> void post(final AsyncRun<T> asyncRun) {
this.handler.post(new Runnable() {
#Override
public final void run() {
MultiThreadHelper.Helper.this.a(asyncRun);
}
});
}
}
public static void post(Runnable runnable) {
Helper.instance.post(runnable);
}
public static void postOnMainThread(Runnable runnable) {
Helper.instance.postOnMainThread(runnable);
}
public static <T> void post(AsyncRun<T> asyncRun) {
Helper.instance.post(asyncRun);
}
And this is the part where I get the 2 errors
public void a(final AsyncRun asyncRun) {
try {
final Object onExecute = asyncRun.onExecute();
this.mainHandler.post(new Runnable() {
#Override
public final void run() {
MultiThreadHelper.AsyncRun.this.onSuccess(onExecute);
}
});
} catch (Exception e2) {
e2.printStackTrace();
this.mainHandler.post(new Runnable() {
#Override
public final void run() {
MultiThreadHelper.AsyncRun.this.onError(e2);
}
});
}
}
What am I doing wrong or is there a solution?

Related

Nested callback is too deep

How to replace nested callbacks (Testing.java) for easy reading like this:
if(isValidGenderID() && isValidReligionID() && isValidMaritalID()){
// DO PRIMARY TASK
}
Nested callbacks are too deep, making the program not easy to read!. How to resolved this problem?
//PersonValidation.java
public static void isValidGenderID(#NonNull Context context, int genderID, final IGenderDataSource.IIsExistGenderIDCallback callback) {
GenderDataSource.getInstance(context).isExistGenderID(genderID, new IGenderDataSource.IIsExistGenderIDCallback() {
#Override
public void onSuccess(boolean result) {
callback.onSuccess(result);
}
#Override
public void onFailure(String result) {
callback.onFailure(result);
}
});
}
public static void isValidReligionID(#NonNull Context context, int religionID, final IReligionDataSource.IIsExistReligionIDCallback callback) {
ReligionDataSource.getInstance(context).isExistReligionID(religionID, new IReligionDataSource.IIsExistReligionIDCallback() {
#Override
public void onSuccess(boolean result) {
callback.onSuccess(result);
}
#Override
public void onFailure(String result) {
callback.onFailure(result);
}
});
}
public static void isValidMaritalID(#NonNull Context context, int maritalID, final IMaritalDataSource.IIsExistMaritalIDCallback callback) {
MaritalDataSource.getInstance(context).isExistMaritalID(maritalID, new IMaritalDataSource.IIsExistMaritalIDCallback() {
#Override
public void onSuccess(boolean result) {
callback.onSuccess(result);
}
#Override
public void onFailure(String result) {
callback.onFailure(result);
}
});
}
// GenderDataSource.java
#Override
public void isExistGenderID(final int ID, #NonNull final IIsExistGenderIDCallback callback) {
Runnable r = new Runnable() {
#Override
public void run() {
db.sqlRawQuery();
callback.onSuccess();
}
};
appExecutors.getLocalDb().execute(r);
}
// ReligionDataSource.java
#Override
public void isExistReligionID(final int ID, #NonNull final IIsExistReligionIDCallback callback) {
Runnable r = new Runnable() {
#Override
public void run() {
db.sqlRawQuery();
callback.onSuccess();
}
};
appExecutors.getLocalDb().execute(r);
}
// MaritalDataSource.java
#Override
public void isExistMaritalID(final int ID, #NonNull final IIsExistMaritalIDCallback callback) {
Runnable r = new Runnable() {
#Override
public void run() {
db.sqlRawQuery();
callback.onSuccess();
}
};
appExecutors.getLocalDb().execute(r);
}
// Testing.java (Nested calls are too deep, making the program not easy to read)
#Override
public void createCustomer(#NonNull final Customer customer, #NonNull final ICustomerDataSource.ICreateCustomerCallback callback) {
//
// isValidGenderID?
//
isValidGenderID(context, customer.getGenderID(), new IGenderDataSource.IIsExistGenderIDCallback() {
#Override
public void onSuccess(boolean result) {
if (result) {
//
// isValidReligionID?
//
isValidReligionID(context, customer.getReligionID(), new IReligionDataSource.IIsExistReligionIDCallback() {
#Override
public void onSuccess(boolean result) {
if (result) {
//
// isValidMaritalID?
//
isValidMaritalID(context, customer.getMaritalID(), new IMaritalDataSource.IIsExistMaritalIDCallback() {
#Override
public void onSuccess(boolean result) {
if (result) {
//
// DO PRIMARY TASK
//
} else {
callback.onFailure("Marital is not valid");
}
}
#Override
public void onFailure(String result) {
callback.onFailure(result);
}
});
//
} else {
callback.onFailure("Religion is not valid!");
}
}
#Override
public void onFailure(String result) {
callback.onFailure(result);
}
});
//
} else {
callback.onFailure("Gender is not valid!");
}
}
#Override
public void onFailure(String result) {
callback.onFailure(result);
}
});
}

Android : CountDownLatch Doesn't work

Following is my class which uses a CountDownLatch and ExecutorService. I all startTest method from my activity. When I run this code CountdownLatch doesn't come out of wait.
public class Test implements Tester.StartListener, Tester.StopListener, Runnable{
private CountDownLatch latch;
ExecutorService executorService;
Context context;
ListElement listElement;
int countDown;
final String user = "palapi_android#tourmalinelabs.com";
public Test(Context context) {
this.context = context;
this.listElement = listElement;
}
public void startTest(int nTimes) throws InterruptedException {
this.countDown = nTimes;
executorService = Executors.newSingleThreadExecutor();
latch = new CountDownLatch(countDown);
executorService.execute(this);
latch.await();
}
#Override
public void run() {
setText("Running");
Tester.Start(context, user, this);
}
#Override
public void OnStarted() {
Tester.Stop(context, this);
}
#Override
public void OnFail( int reason ) {
Log.d("Failure", "failed because " + reason);
}
#Override
public void OnStopped() {
setText(String.valueOf(countDown));
countDown --;
latch.countDown();
if( countDown >= 0) {
runAfterDelayInRange(new Runnable() {
#Override
public void run() {
Tester.Start(context, user, Test.this);
}
});
} else {
setText("Done");
}
}
protected void setText(final String text) {
getHandler().post(new Runnable() {
#Override
public void run() {
if (listElement != null) {
listElement.setTestResult(text);
}
LauncherActivity activity = (LauncherActivity) context;
activity.notifyDataSetChanged();
}
});
}
protected void runAfterDelayInRange(Runnable action) {
getHandler().post(action);
}
private Handler getHandler(){
return new Handler(context.getMainLooper());
}
}
Can someone please tell me what am I doing wrong? Thank you in advance

create own Listener in android

i want to add listener to my class below :
class BitmapDisplay implements Runnable
{
IAsyncFetchListener fetchListener = null;
public void setListener(IAsyncFetchListener listener) {
this.fetchListener = listener;
}
Bitmap bitmap;
BitmapToLoad bitmapToLoad;
public BitmapDisplay(Bitmap b, BitmapToLoad p){bitmap=b;bitmapToLoad=p;}
public void run()
{
if(bitmap!=null)
returnbitmap=bitmap;
else
returnbitmap=BitmapFactory.decodeResource(context.getResources(), stub_id);
this.fetchListener.onComplete(returnbitmap);
}
}
}
but my listener wont work in eclipse
imageLoader.DisplayBitmap("").setListener(new IAsyncFetchListener() {
#Override
public void onComplete(Bitmap bitmap) {
photoView.setImageBitmap(bitmap);
}
});
I get an error in setListener :
The method setListener(new IAsyncFetchListener(){}) is undefined for
the type Bitmap" how to solve it?
Because when you do imageLoader.DisplayBitmap(""), the method DisplayBitmap("") return a Bitmap.
You are mixing the method DisplayBitmap with the object BitmapDisplay.
But you can be able to do that :
BitmapDisplay bd=new BitmapDisplay(bmp, bitmapToLoad);
bd.setListener(new IAsyncFetchListener() {
#Override
public void onComplete(Bitmap bitmap) {
photoView.setImageBitmap(bitmap);
}
}
);
Change your code to following:
DisplayBitmap function
public Void DisplayBitmap(String url,IAsyncFetchListener listener)
{
Bitmap bitmap=memoryCache.get(url);
if(bitmap!=null)
listener.onComplete(bitmap);
else
{
queueBitmap(url,listener);
listener.onComplete(BitmapFactory.decodeResource(context.getResources(), stub_id));
}
}
queueBitmap function
private void queueBitmap(String url,IAsyncFetchListener listener)
{
BitmapToLoad p=new BitmapToLoad(url);
executorService.submit(new BitmapsLoader(p,listener));
}
BitmapsLoader constructor
IAsyncFetchListener listener;
BitmapsLoader(BitmapToLoad bitmapToLoad,IAsyncFetchListener listener){
this.bitmapToLoad=bitmapToLoad;
this.listener=listener;
}
Run method
#Override
public void run() {
try{
Bitmap bmp=getBitmap(bitmapToLoad.url);
memoryCache.put(bitmapToLoad.url, bmp);
BitmapDisplay bd=new BitmapDisplay(bmp, bitmapToLoad);
bd.setListener(listener);
handler.post(bd);
}catch(Throwable th){
th.printStackTrace();
}
}
And finally call this:
imageLoader.DisplayBitmap("",new IAsyncFetchListener() {
#Override
public void onComplete(Bitmap bitmap) {
photoView.setImageBitmap(bitmap);
}
});

cant connect to facebook with a network thread

I need to connect to facebook so I use a Tread when using the network. but I have a problem:
Thread t = new Thread(new Runnable() {
#Override
public void run() {
FacebookConnectTask task = new FacebookConnectTask("facebookId", "token", "email", facebookGender,0, 0);
task.setOnPreExecuteListener(this);
task.setOnDoneListener(this);
task.execute();
}
});
t.start();
}
I cant do
task.setOnPreExecuteListener(this);
task.setOnDoneListener(this);
eclipse gives me this error: "The method setOnDoneListener(Task.OnDoneListener) in the type Task is not applicable for the arguments (new Runnable(){})"
How can i fix this?
Thanks!
You've changed context's by being in a Thread your in an Annoyomous class, this is now your annonomous class and not the outer class.
Try this (pun intended):
task.setOnPreExecuteListener(YourOuterClass.this);
task.setOnDoneListener(YourOuterClass.this);
i.e.
public class YourClass implements OnDoneListener {
public doFacebook(){
new Thread(new Runnable(){
#Override
public void run(){
task.setOnDoneListener(YourClass.this);
}
}.start();
}
#Override
public void onDone(){
}
}
or alternatively pull your Threaded class out:
public class DoSomething implements Runnable {
private final OnDoneListener listener;
public DoSomething(OnDoneListener listener){
this.listener = listener;
}
#Override
public void run(){
FacebookConnectTask task = ... ;
task.setOnDoneListener(listener);
}
}
public class YourActivity extends Activity implements OnDoneListener {
public void onCreate(Bundle b){
new Thread(new DoSomething(this)).start();
}
#Override
public void onDone(){
// Tada
}
}
A further step if you wanted to be cooler is create your own interface and keep all the Facebook stuff in the runnable class:
public class DoSomething implements Runnable, OnDoneListener {
public interface OnFacebookFinished {
void onFacebookFinished();
}
private final OnFacebookFinished listener;
public DoSomething(OnFacebookFinished listener){
this.listener = listener;
}
#Override
public void run(){
FacebookConnectTask task = ... ;
task.setOnDoneListener(this);
}
#Override
public void onDone(){
if(listener != null){
listener.onFacebookFinished();
}
}
}
public class YourActivity extends Activity implements OnFacebookFinished {
#Override
public void onCreate(Bundle b){
new Thread(new DoSomething(this)).start();
}
#Override
public void onFacebookFinished(){
// Tada
}
}

Why aren't all my listeners registered?

public interface DownloadListener {
public void onDownloaded();
}
public class DownloadManager {
private static DownloadManager instance;
private DownloadListener mDownloadListener;
public static synchronized DownloadManager getInstance(){
if(instance == null)
instance = new DownloadManager();
return instance;
}
private DownloadManager() {
myHandler.sendEmptyMessageDelayed(29, 3 * 1000);
}
public void registerDownloadListener(DownloadListener downloadListener) {
mDownloadListener = downloadListener;
}
Handler myHandler = new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
if (msg.what == 29) {
mDownloadListener.onDownloaded();
return true;
}
return false;
}
});
}
public class I implements DownloadListener {
public I() {
DownloadManager.getInstance().registerDownloadListener(this);
}
#Override
public void onDownloaded() {
Log.e("TAG", "I onDownloaded");
}
}
public class You implements DownloadListener {
public You() {
DownloadManager.getInstance().registerDownloadListener(this);
}
#Override
public void onDownloaded() {
Log.e("TAG", "You onDownloaded");
}
}
public class PATTERNSActivity extends Activity implements DownloadListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new I();
new You();
DownloadManager.getInstance().registerDownloadListener(this);
}
#Override
public void onDownloaded() {
Log.e("TAG","PATTERNSActivity onDownloaded");
}
}
I am expecting to get:
I onDownloaded
You onDownloaded
PATTERNSActivity onDownloaded
But I am getting only:
PATTERNSActivity onDownloaded
What could it be the problem?
You keep registered downloaders in a single instance property:
// Last call's downloadListener wins.
public void registerDownloadListener(DownloadListener downloadListener) {
mDownloadListener = downloadListener;
}
The last one registered is the activity's:
new I(); // First set singleton's property to an instance of I...
new You(); // ...then to an instance of You...
// ...then to the current instance.
DownloadManager.getInstance().registerDownloadListener(this);
Edit based on your comment.
public void registerDownloadListener(DownloadListener downloadListener) {
mDownloadListeners.add(downloadListener);
}
...
public boolean handleMessage(Message msg) {
if (msg.what != 29) {
return false;
}
for (DownloadListener listener : mDownloadListeners) {
listener.onDownloaded();
}
return true;
}
In your code, this gets executed by calling mDownloadListener.onDownloaded(); in the DownloadManager class.
#Override
public void onDownloaded() {
Log.e("TAG","PATTERNSActivity onDownloaded");
}
In don't see why the onDownloaded methods of the I and YOU class should be executed, they're never called. Only the OnDownloaded method of your Listener is called.
For starters, I think you are not using a list. You just override the value so you will always get the last one:
public void registerDownloadListener(DownloadListener downloadListener) {
mDownloadListener = downloadListener;
}

Categories

Resources