Java Code Examples for com.codename1.ui.Display#isInitialized()

The following examples show how to use com.codename1.ui.Display#isInitialized() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CodenameOneBackgroundFetchActivity.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void onStart() {
    super.onStart();
    if(!Display.isInitialized()) {
        // Since callingStartActivity() on this activity automatically launches
        // the CodenameOneStub (the main activity), the Display should be initialized by this point.
        // It it is not initialized, then this is an exceptional case.  Just log it so we
        // know it isn't initializing
        Log.d("Codename One", "Display not initialized in CodenameOneBackgroundFetchActivity.  Skipping background fetch");
        finish();
        return;
    }
    
    try {
        AndroidImplementation.performBackgroundFetch(false);
    } catch (Exception e) {
        Log.e("Codename One", "Background fetch error", e);
    } finally {
        finish();
    }
}
 
Example 2
Source File: CodenameOneInputConnection.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean performEditorAction(int actionCode) {
    if (Display.isInitialized() && Display.getInstance().getCurrent() != null) {
        Component txtCmp = Display.getInstance().getCurrent().getFocused();
        if (txtCmp != null && txtCmp instanceof TextField) {
            TextField t = (TextField) txtCmp;
            if (actionCode == EditorInfo.IME_ACTION_DONE) {
                Display.getInstance().setShowVirtualKeyboard(false);
            } else if (actionCode == EditorInfo.IME_ACTION_NEXT) {
                Display.getInstance().setShowVirtualKeyboard(false);
                txtCmp.getNextFocusDown().requestFocus();
            }
        }
    }

    return super.performEditorAction(actionCode);
}
 
Example 3
Source File: CodenameOneInputConnection.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
@Override
public CharSequence getTextBeforeCursor(int length, int flags) {
    if (Display.isInitialized() && Display.getInstance().getCurrent() != null) {
        Component txtCmp = Display.getInstance().getCurrent().getFocused();
        if (txtCmp != null && txtCmp instanceof TextField) {
            String txt = ((TextField) txtCmp).getText();
            int position = ((TextField) txtCmp).getCursorPosition();
            int start;
            if (position > 0) {
                start = txt.subSequence(0, position).toString().lastIndexOf(" ");
                if (start > 0) {
                    return txt.subSequence(start, position);
                } else {
                    return txt.subSequence(0, position);
                }
            }
        }
    }
    return "";
}
 
Example 4
Source File: CodenameOneInputConnection.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean setComposingText(CharSequence text, int newCursorPosition) {
    if (Display.isInitialized() && Display.getInstance().getCurrent() != null) {
        Component txtCmp = Display.getInstance().getCurrent().getFocused();
        if (txtCmp != null && txtCmp instanceof TextField) {
            TextField t = (TextField) txtCmp;
            String textFieldText = t.getText();
            StringBuilder sb = new StringBuilder(textFieldText);
            sb.replace(sb.length() - composingText.length(), sb.length(), text.toString());
            int cursorPosition = t.getCursorPosition();
            composingText = text.toString();
            t.setText(sb.toString());
            t.setCursorPosition(cursorPosition + text.length());

            updateExtractedText();
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: AndroidTextureView.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    if (!Display.isInitialized()) {
        return;
    }
    Display.getInstance().callSerially(new Runnable() {

        public void run() {
            cn1View.handleSizeChange(w, h);
        }
    });
}
 
Example 6
Source File: Log.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sends the current log to the cloud regardless of the reporting level
 */
private static void sendLogImpl(boolean sync) {
    try {
        // this can cause a crash
        if(!Display.isInitialized()) {
            return;
        }
        if(!instance.logDirty) {
            return;
        }
        instance.logDirty = false;
        String devId = getUniqueDeviceKey();
        if(devId == null) {
            if(Display.getInstance().isSimulator()) {
                Dialog.show("Send Log Error", "Device Not Registered: Sending a log from an unregistered device is impossible", "OK", null);
            } else {
                Log.p("Device Not Registered: Sending a log from an unregistered device is impossible");
            }
            return;
        }
        ConnectionRequest r = new ConnectionRequest();
        r.setPost(false);
        MultipartRequest m = new MultipartRequest();
        m.setUrl("https://crashreport.codenameone.com/CrashReporterEmail/sendCrashReport");
        byte[] read = Util.readInputStream(Storage.getInstance().createInputStream("CN1Log__$"));
        m.addArgument("i", "" + devId);
        m.addArgument("u",Display.getInstance().getProperty("built_by_user", ""));
        m.addArgument("p", Display.getInstance().getProperty("package_name", ""));
        m.addArgument("v", Display.getInstance().getProperty("AppVersion", "0.1"));
        m.addData("log", read, "text/plain");
        m.setFailSilently(true);
        if(sync) {
            NetworkManager.getInstance().addToQueueAndWait(m);
        } else {
            NetworkManager.getInstance().addToQueue(m);
        }
    } catch (Throwable ex) {
        ex.printStackTrace();
    }
}
 
Example 7
Source File: AndroidSurfaceView.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {

    if (!Display.isInitialized() || Display.getInstance().getCurrent() == null) {
        return super.onCreateInputConnection(editorInfo);
    }
    cn1View.setInputType(editorInfo);
    return super.onCreateInputConnection(editorInfo);
}
 
Example 8
Source File: ConnectionRequest.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Handles an exception thrown when performing a network operation, the default
 * implementation shows a retry dialog.
 *
 * @param err the exception thrown
 */
protected void handleException(Exception err) {
    if(exceptionListeners != null) {
        if(!isKilled()) {
            NetworkEvent n = new NetworkEvent(this, err);
            exceptionListeners.fireActionEvent(n);
        }
        return;
    }
    if(killed || failSilently) {
        failureException = err;
        return;
    }
    Log.e(err);
    if(silentRetryCount > 0) {
        silentRetryCount--;
        NetworkManager.getInstance().resetAPN();
        retry();
        return;
    }
    if(Display.isInitialized() && !Display.getInstance().isMinimized() &&
            Dialog.show("Exception", err.toString() + ": for URL " + url + "\n" + err.getMessage(), "Retry", "Cancel")) {
        retry();
    } else {
        retrying = false;
        killed = true;
    }
}
 
Example 9
Source File: CodenameOneActivity.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    this.menu = menu;
    // By returning true we signal let Android know that we want the menu
    // to be displayed
    return nativeMenu && Display.isInitialized() && Display.getInstance().getCurrent() != null;
}
 
Example 10
Source File: CodenameOneInputConnection.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
    if (Display.isInitialized() && Display.getInstance().getCurrent() != null) {
        this.request = request;
        ExtractedText et = new ExtractedText();
        if (extractText(request, et)) {
            return et;
        }
    }
    return null;
}
 
Example 11
Source File: CodenameOneInputConnection.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CharSequence getTextAfterCursor(int length, int flags) {
    if (Display.isInitialized() && Display.getInstance().getCurrent() != null) {
        Component txtCmp = Display.getInstance().getCurrent().getFocused();
        if (txtCmp != null && txtCmp instanceof TextField) {
            String txt = ((TextField) txtCmp).getText();
            int position = ((TextField) txtCmp).getCursorPosition();
            if (position > -1 && position < txt.length()) {
                return txt.subSequence(position, txt.length() - 1);
            }
        }
    }
    return "";
}
 
Example 12
Source File: CodenameOneInputConnection.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
    
    if (Display.isInitialized() && Display.getInstance().getCurrent() != null) {
        Component txtCmp = Display.getInstance().getCurrent().getFocused();
        if (txtCmp != null && txtCmp instanceof TextField) {
            TextField t = (TextField) txtCmp;
            String textFieldText = t.getText();
            int cursorPosition = t.getCursorPosition();
            StringBuilder sb = new StringBuilder(textFieldText);
            if (text.equals("\n")) {
                //System.out.println("hello backslash");
            }

            if (composingText.length() > 0) {
                if (text.equals(" ")) {
                    return commitText(composingText + " ", newCursorPosition);
                }
                sb.replace(sb.length() - composingText.length(), sb.length(), text.toString());
                composingText = "";
            } else {
                sb.insert(cursorPosition, text);
            }

            t.setText(sb.toString());
            t.setCursorPosition(cursorPosition + text.length());

            updateExtractedText();
        }
    }
    return super.commitText(text, newCursorPosition);
}
 
Example 13
Source File: CodenameOneInputConnection.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public Editable getEditable() {
    if (Display.isInitialized() && Display.getInstance().getCurrent() != null) {
        Component txtCmp = Display.getInstance().getCurrent().getFocused();
        if (txtCmp != null && txtCmp instanceof TextField) {
            TextField t = (TextField) txtCmp;
            String textFieldText = t.getText();
            edit.clear();
            edit.append(textFieldText);
            return edit;
        }
    }
    return super.getEditable();
}
 
Example 14
Source File: AndroidTextureView.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onCheckIsTextEditor() {
    if (!Display.isInitialized() || Display.getInstance().getCurrent() == null) {
        return false;
    }
    Component txtCmp = Display.getInstance().getCurrent().getFocused();
    if (txtCmp != null && txtCmp instanceof TextField) {
        return true;
    }
    return false;
}
 
Example 15
Source File: AndroidAsyncView.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onCheckIsTextEditor() {
    if (!Display.isInitialized() || Display.getInstance().getCurrent() == null) {
        return false;
    }
    Component txtCmp = Display.getInstance().getCurrent().getFocused();
    if (txtCmp != null && txtCmp instanceof TextField) {
        return true;
    }
    return false;
}
 
Example 16
Source File: AndroidAsyncView.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
    if (!Display.isInitialized() || Display.getInstance().getCurrent() == null) {
        return super.onCreateInputConnection(editorInfo);
    }
    cn1View.setInputType(editorInfo);
    return super.onCreateInputConnection(editorInfo);
}
 
Example 17
Source File: AndroidTextureView.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {

    if (!Display.isInitialized() || Display.getInstance().getCurrent() == null) {
        return super.onCreateInputConnection(editorInfo);
    }
    cn1View.setInputType(editorInfo);
    return super.onCreateInputConnection(editorInfo);
}
 
Example 18
Source File: CodenameOneBackgroundLocationActivity.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onStart() {
    super.onStart();
    if(!Display.isInitialized()) {
        //Display.init(this);
        // This should never happen because Android will load the main activity first
        // automatically when we call startActivity()... and that will initialize the display
        Log.d("CN1", "Display is not initialized.  Cannot deliver background location update");
        finish();
        
        return;
    }
    Bundle b = getIntent().getExtras();
    if(b != null){
        String locationClass = b.getString("backgroundLocation");
        Location location = b.getParcelable("Location");
        try {
            //the 2nd parameter is the class name we need to create
            LocationListener l = (LocationListener) Class.forName(locationClass).newInstance();
            l.locationUpdated(AndroidLocationManager.convert(location));
        } catch (Exception e) {
            Log.e("Codename One", "background location error", e);
        }
        
    }
    //finish this activity once the Location has been handled
    finish();
}
 
Example 19
Source File: BackgroundFetchHandler.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    String[] params = intent.getDataString().split("[?]");
    if (!Display.isInitialized()) {
        shouldStopContext = true;
        AndroidImplementation.startContext(this);
        try {
            Class cls = Class.forName(params[1]);
            BackgroundFetch obj = (BackgroundFetch)cls.newInstance();
            AndroidImplementation.backgroundFetchListener = obj;
           
        } catch (Exception ex) {
            Log.d("CN1", "Failed to instantiate background fetch class "+params[1]);
            ex.printStackTrace();
            return;
        }

    } 

    try {
        AndroidImplementation.performBackgroundFetch(shouldStopContext);
    } catch (Exception e) {
        Log.e("Codename One", "background fetch error", e);
    }

    if (shouldStopContext) {
        AndroidImplementation.stopContext(this);
    }
}
 
Example 20
Source File: BackgroundLocationHandler.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    //String className = intent.getStringExtra("backgroundClass");
    final android.location.Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
    if (AndroidLocationPlayServiceManager.inMemoryBackgroundLocationListener != null) {
        // This is basically just a short-term location request and we are using the in-memory listeners.
        AndroidLocationPlayServiceManager mgr = AndroidLocationPlayServiceManager.inMemoryBackgroundLocationListener;
        mgr.onLocationChanged(location);
        return;
    }
    if (intent.getDataString() == null) {
        System.out.println("BackgroundLocationHandler received update without data string.");
        return;
    }
    String[] params = intent.getDataString().split("[?]");
    

    //might happen on some occasions, no need to do anything.
    if (location == null) {
        return;
    }
    //if the Display is not initialized we need to launch the CodenameOneBackgroundLocationActivity 
    //activity to handle this
    boolean shouldStopWhenDone = false;
    if (!Display.isInitialized()) {
        shouldStopWhenDone = true;
        AndroidImplementation.startContext(this);
        //Display.init(this);
        /*
        Intent bgIntent = new Intent(getBaseContext(), CodenameOneBackgroundLocationActivity.class);
        Bundle b = new Bundle();
        b.putString("backgroundLocation", params[1]);
        b.putParcelable("Location", location);
        bgIntent.putExtras(b); //Put your id to your next Intent
        bgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(bgIntent);
                */
    } //else {

    try {
        //the 2nd parameter is the class name we need to create
        LocationListener l = (LocationListener) Class.forName(params[1]).newInstance();
        l.locationUpdated(AndroidLocationManager.convert(location));
    } catch (Exception e) {
        Log.e("Codename One", "background location error", e);
    }
    if (shouldStopWhenDone) {
        AndroidImplementation.stopContext(this);
    }
    //}
}